我正在使用 glob 函数来加载
#make a list from pdb filles located in the same directory as this python script
pdb_list = glob.glob('*.pdb')
# do something on each pdb file
for pdb in pdb_list:
some_variable = some_function(pdb)
我怎样才能使用特定的 glob 路径,例如直接从某个特定的子目录加载所有这些 pdb 填充,该子目录与我的 python 脚本位于同一位置,然后返回到初始目录(在那里产生一些输出)?
The one way that I found is to use
# change current directory to ./pdb
os.chdir("pdb")
但是,在这种情况下,所有内容(包括保存结果)都将发生在包含所有初始 PDB 填充的目录中。是否可以表示 glob 在 pdb 文件夹中查找填充物但始终保持在初始位置?
glob
也适用于相对路径,因此您可以简单地使用pdb/*.pdb
它并且它会起作用 -glob
将返回相对于您当前目录的结果:
>>> glob.glob('test/*.txt')
['test/c.txt', 'test/b.txt', 'test/a.txt']
如果您需要没有目录的结果,您可以随时使用os.path.basename
仅获取文件名:
>>> [os.path.basename(p) for p in glob.glob('test/*.txt')]
['c.txt', 'b.txt', 'a.txt']
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句