您的位置:首页 > 编程语言 > Python开发

python 文件批处理

2016-07-12 14:18 453 查看
You can list all files in the current directory using:

import os
for filename in os.listdir(os.getcwd()):
# do your stuff


Or you can list only some files, depending on the file pattern using the glob module:

import glob
for filename in glob.glob('*.txt'):
# do your stuff


It doesn’t have to be the current directory you can list them in any path you want:

path = '/some/path/to/file'

for filename in os.listdir(path):
# do your stuff


for filename in glob.glob(os.path.join(path, '*.txt')):
# do your stuff


Or you can even use the pipe as you specified using fileinput

import fileinput
for line in fileinput.input():
# do your stuff


And then use it with piping:

ls -1 | python parse.py
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python