B.M*_*.W. 124 python stdin stdout file pipe
我有一个python脚本parse.py,它在脚本中打开一个文件,比如file1,然后做一些事情可能会打印出总字符数.
filename = 'file1'
f = open(filename, 'r')
content = f.read()
print filename, len(content)
Run Code Online (Sandbox Code Playgroud)
现在,我使用stdout将结果定向到我的输出文件 - 输出
python parse.py >> output
Run Code Online (Sandbox Code Playgroud)
但是,我不想手动执行此文件,有没有办法自动处理每个文件?喜欢
ls | awk '{print}' | python parse.py >> output
Run Code Online (Sandbox Code Playgroud)
那么问题是如何从standardin读取文件名?或者已经有一些内置函数可以轻松完成ls和那些工作?
谢谢!
Vik*_*kez 309
您可以使用以下命令列出当前目录中的所有文件:
import os
for filename in os.listdir(os.getcwd()):
# do your stuff
Run Code Online (Sandbox Code Playgroud)
或者您只能列出一些文件,具体取决于使用glob模块的文件模式:
import glob
for filename in glob.glob('*.txt'):
# do your stuff
Run Code Online (Sandbox Code Playgroud)
它不必是您可以在任何所需路径中列出它们的当前目录:
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
Run Code Online (Sandbox Code Playgroud)
或者您甚至可以使用您指定的管道 fileinput
import fileinput
for line in fileinput.input():
# do your stuff
Run Code Online (Sandbox Code Playgroud)
然后用它来管道:
ls -1 | python parse.py
Run Code Online (Sandbox Code Playgroud)
le_*_*ine 31
你应该尝试使用os.walk
yourpath = 'path'
import os
for root, dirs, files in os.walk(yourpath, topdown=False):
for name in files:
print(os.path.join(root, name))
stuff
for name in dirs:
print(os.path.join(root, name))
stuff
Run Code Online (Sandbox Code Playgroud)
小智 8
我一直在寻找这个答案:
import os,glob
folder_path = '/some/path/to/file'
for filename in glob.glob(os.path.join(folder_path, '*.htm')):
with open(filename, 'r') as f:
text = f.read()
print (filename)
print (len(text))
Run Code Online (Sandbox Code Playgroud)
您也可以选择'*.txt'或文件名的其他两端
import os #os module imported here
location = os.getcwd() # get present working directory location here
counter = 0 #keep a count of all files found
csvfiles = [] #list to store all csv files found at location
filebeginwithhello = [] # list to keep all files that begin with 'hello'
otherfiles = [] #list to keep any other file that do not match the criteria
for file in os.listdir(location):
try:
if file.endswith(".csv"):
print "csv file found:\t", file
csvfiles.append(str(file))
counter = counter+1
elif file.startswith("hello") and file.endswith(".csv"): #because some files may start with hello and also be a csv file
print "csv file found:\t", file
csvfiles.append(str(file))
counter = counter+1
elif file.startswith("hello"):
print "hello files found: \t", file
filebeginwithhello.append(file)
counter = counter+1
else:
otherfiles.append(file)
counter = counter+1
except Exception as e:
raise e
print "No files found here!"
print "Total files found:\t", counter
Run Code Online (Sandbox Code Playgroud)
现在,您不仅列出了文件夹中的所有文件,还将它们(可选)按起始名称,文件类型等进行排序.刚才迭代每个列表并做你的东西.