如何根据列表元素搜索文件?
How can I search for files based on elements of a list?
我有一个列表 tile
包含这些元素
G11451-11500
G11501-11550
G11551-11600
G11601-11650
我想用它来搜索目录并将文件存储在列表中。
例如,这:glob.glob('*G11451-11500*.sh')
为我提供了包含文件名列表的预期输出。
但我想循环执行此操作。我无法从这里继续:
for ix in tile:
e = glob.glob('*format(ix)*')
使用 str.format
并将 ix
传递到字符串中:
e = glob.glob('*{}*.sh'.format(ix))
如果您从某些外部源(例如文件)读取这些字符串,请确保您已使用 str.strip
.
删除尾随的换行符
我有一个列表 tile
包含这些元素
G11451-11500
G11501-11550
G11551-11600
G11601-11650
我想用它来搜索目录并将文件存储在列表中。
例如,这:glob.glob('*G11451-11500*.sh')
为我提供了包含文件名列表的预期输出。
但我想循环执行此操作。我无法从这里继续:
for ix in tile:
e = glob.glob('*format(ix)*')
使用 str.format
并将 ix
传递到字符串中:
e = glob.glob('*{}*.sh'.format(ix))
如果您从某些外部源(例如文件)读取这些字符串,请确保您已使用 str.strip
.