我有以下python脚本:
with open('ein.csv', 'r') as istr:
with open('aus.csv', 'w') as ostr:
for line in istr:
line = line.rstrip('\n') + ',1'
print(line, file=ostr)
如何将其推广到目录中的所有文件并为每个文件输出一个单独的文件?
可能有这样的功能:
for phyle in list_files(.):
with open(phyle, 'r') as istr:
with open('ausput_xyz.csv', 'w') as ostr:
for line in istr:
line = line.rstrip('\n') + ',1'
print(line, file=ostr)
def list_files(path):
# returns a list of names (with extension, without full path) of all files
# in folder path
files = []
for name in os.listdir(path):
if os.path.isfile(os.path.join(path, name)):
files.append(name)
return files
答案 0 :(得分:2)
只需将代码放入函数中并调用它:
def process(infilename):
outfilename = os.path.splitext(infilename)[0] + "-out.csv"
with open(infilename, 'r') as istr:
with open(outfilename, 'w') as ostr:
for line in istr:
line = line.rstrip('\n') + ',1'
print(line, file=ostr)
def process_files(path):
for name in os.listdir(path):
if os.path.isfile(os.path.join(path, name)):
process(name)
在包含输入文件" abc.csv"," xyz.csv"的目录中此代码将创建名为" abc-out.csv"的输出文件。和" xyz-out.csv"。
请注意,os.listdir(path)
在执行期间只调用一次,因此要处理的文件列表将不包括新创建的输出文件。
答案 1 :(得分:2)
首先,作为处理csv
文件的更加pythonic方式,您最好使用csv
模块并使用with
语句打开将自动关闭文件对象的文件块结束。并使用os.walk()
函数迭代特定路径的文件和目录:
import csv
import os
for path_name, dirs, files in os.walk('relative_path'):
for file_name in files:
with open(file_name) as inp,open('{}.csv'.format(file_name),'wb') as out:
spamwriter = csv.writer(out, delimiter=',')
for line in inp:
spamwriter.writerow(line) # or line.split() with a specific delimiter
请注意,如果您的脚本与文件目录不在同一路径中,则可以在要打开文件时添加路径到文件名前导。
with open(path_name+'/'+file_name),...