1

我希望在其上运行脚本的目录中有无数个文件。它们都有一个类似的文件名:prefix_foo_123456_asdf_asdfasdf.csv。我知道如何使用shell中文件名中的变量而不是python来循环目录中的文件。有没有相应的方法来做类似的事情

$i=0

for $i<100

./process.py prefix_foo_$i_*

$i++

endloop
4

3 回答 3

4

您可以使用glob.globglob.iglob获取文件名的列表/迭代器。

例如,如果您的目录有“file1.txt”、“file2.txt”、“file3.txt”

import glob
print (glob.glob('*.txt'))  #['file1.txt','file2.txt','file3.txt']

虽然列表不一定是排序的。

你的循环可以写成:

import subprocess
import glob
for i in range(100):
    files=glob.glob('prefix_foo_%d_*'%(i))
    subprocess.call(['./process.py']+files)

当然,在 python 中使用 subprocess 来运行另一个 python 程序可能不是最好的设计......(你可能会从另一个模块导入你需要的东西并运行它而不产生另一个进程)

于 2012-06-11T19:09:55.833 回答
2

使用标准库 glob。假设 process.py 的功能在函数 process_one_file 中:

from glob import glob
from process import process_one_file

for i in range(100):
    process_one_file(glob('prefix_foo_{}_*'.format(i)))
于 2012-06-11T19:15:24.600 回答
2

其他方式:

from os import walk

>>> for filename, subdirs, dirs in walk('/home'):
...     print (filename, subdirs, dirs)

输出:

home/di/workspace/local2stream/mediaelement/.git/info [] ['exclude'] /home/di/workspace/local2stream/mediaelement/.git/logs ['refs'] ['HEAD'] /home/di /workspace/local2stream/mediaelement/.git/logs/refs ['remotes', 'heads'] [] /home/di/workspace/local2stream/mediaelement/.git/logs/refs/remotes ['origin'] [] /home/di/workspace/local2stream/mediaelement/.git/logs/refs/remotes/origin [] ['HEAD'] /home/di/workspace/local2stream/mediaelement/.git/logs/refs/heads [] [ 'master'] /home/di/workspace/local2stream/mediaelement/.git/objects ['info', 'pack'] [] /home/di/workspace/local2stream/mediaelement/.git/objects/info [] [ ] /home/di/workspace/local2stream/mediaelement/.git/objects/pack [] ['pack-a378eaa927a4825f049faf10bab35cf5d94545f1.idx','pack-a378eaa927a4825f049faf10bab35cf5d94545f1.pack'] /home/di/workspace/local2stream/mediaelement/.git/refs ['tags', 'remotes', 'heads'] [] /home/di/workspace/local2stream/mediaelement/. git/refs/tags [] [] /home/di/workspace/local2stream/mediaelement/.git/refs/remotes ['origin'] [] /home/di/workspace/local2stream/mediaelement/.git/refs/remotes /origin [] ['HEAD'] /home/di/workspace/local2stream/mediaelement/.git/refs/heads [] ['master']] [] /home/di/workspace/local2stream/mediaelement/.git/refs/remotes/origin [] ['HEAD'] /home/di/workspace/local2stream/mediaelement/.git/refs/heads [] ['掌握']] [] /home/di/workspace/local2stream/mediaelement/.git/refs/remotes/origin [] ['HEAD'] /home/di/workspace/local2stream/mediaelement/.git/refs/heads [] ['掌握']

于 2012-06-11T19:19:21.307 回答