我想为此编写一个程序:在一个文件夹中,我有 n 个文件数;首先读取一个文件并执行一些操作,然后将结果存储在单独的文件中。然后读取第二个文件,再次执行操作并将结果保存到新的第二个文件中。对 n 数量的文件执行相同的过程。程序逐个读取所有文件,并分别存储每个文件的结果。请举例说明我是如何做到的。
答案 0 :(得分:12)
import sys
# argv is your commandline arguments, argv[0] is your program name, so skip it
for n in sys.argv[1:]:
print(n) #print out the filename we are currently processing
input = open(n, "r")
output = open(n + ".out", "w")
# do some processing
input.close()
output.close()
然后称之为:
./foo.py bar.txt baz.txt
答案 1 :(得分:9)
我认为您错过的是如何检索该目录中的所有文件。 为此,请使用glob模块。 下面是一个示例,它将扩展名为* .txt的所有文件复制到扩展名为* .out
的文件中import glob
list_of_files = glob.glob('./*.txt') # create the list of file
for file_name in list_of_files:
FI = open(file_name, 'r')
FO = open(file_name.replace('txt', 'out'), 'w')
for line in FI:
FO.write(line)
FI.close()
FO.close()
答案 2 :(得分:7)
您可能会发现fileinput
模块很有用。它专为解决这个问题而设计。
答案 3 :(得分:1)
我最近刚刚学会了os.walk()命令,它可能对你有所帮助。 它允许您沿着目录树结构向下走。
import os
OUTPUT_DIR = 'C:\\RESULTS'
for path, dirs, files in os.walk('.'):
for file in files:
read_f = open(os.join(path,file),'r')
write_f = open(os.path.join(OUTPUT_DIR,file))
# Do stuff
答案 4 :(得分:0)
包含目录或特定文件名参数列表的组合答案:
import sys
import os.path
import glob
def processFile(filename):
fileHandle = open(filename, "r")
for line in fileHandle:
# do some processing
pass
fileHandle.close()
def outputResults(filename):
output_filemask = "out"
fileHandle = open("%s.%s" % (filename, output_filemask), "w")
# do some processing
fileHandle.write('processed\n')
fileHandle.close()
def processFiles(args):
input_filemask = "log"
directory = args[1]
if os.path.isdir(directory):
print "processing a directory"
list_of_files = glob.glob('%s/*.%s' % (directory, input_filemask))
else:
print "processing a list of files"
list_of_files = sys.argv[1:]
for file_name in list_of_files:
print file_name
processFile(file_name)
outputResults(file_name)
if __name__ == '__main__':
if (len(sys.argv) > 1):
processFiles(sys.argv)
else:
print 'usage message'
答案 5 :(得分:0)
from pylab import *
import csv
import os
import glob
import re
x=[]
y=[]
f=open("one.txt",'w')
for infile in glob.glob(('*.csv')):
# print "" +infile
csv23=csv2rec(""+infile,'rb',delimiter=',')
for line in csv23:
x.append(line[1])
# print len(x)
for i in range(3000,8000):
y.append(x[i])
print ""+infile,"\t",mean(y)
print >>f,""+infile,"\t\t",mean(y)
del y[:len(y)]
del x[:len(x)]
答案 6 :(得分:0)
我写了一个博客,讲述如何轻松处理包含大量文件的项目并进行处理。
请仔细阅读它,您将对使用Python处理文件有个不错的主意。
答案 7 :(得分:0)
我知道我在某处看到过这个双 with open()
但不记得在哪里。所以我构建了一个小例子以备不时之需。
""" A module to clean code(js, py, json or whatever) files saved as .txt files to
be used in HTML code blocks. """
from os import listdir
from os.path import abspath, dirname, splitext
from re import sub, MULTILINE
def cleanForHTML():
""" This function will search a directory text files to be edited. """
## define some regex for our search and replace. We are looking for <, > and &
## To replaced with &ls;, > and &. We might want to replace proper whitespace
## chars to as well? (r'\t', ' ') and (f'\n', '<br>')
search_ = ((r'(<)', '<'), (r'(>)', '>'), (r'(&)', '&'))
## Read and loop our file location. Our location is the same one that our python file is in.
for loc in listdir(abspath(dirname(__file__))):
## Here we split our filename into it's parts ('fileName', '.txt')
name = splitext(loc)
if name[1] == '.txt':
## we found our .txt file so we can start file operations.
with open(loc, 'r') as file_1, open(f'{name[0]}(fixed){name[1]}', 'w') as file_2:
## read our first file
retFile = file_1.read()
## find and replace some text.
for find_ in search_:
retFile = sub(find_[0], find_[1], retFile, 0, MULTILINE)
## finally we can write to our newly created text file.
file_2.write(retFile)
答案 8 :(得分:-1)
这东西也可以读取多个文件,我的文件名为fedaralist_1.txt
和federalist_2.txt
,像这样,我有84个文件,直到fedaralist_84.txt
我正在将文件读为f。
for file in filename:
with open(f'federalist_{file}.txt','r') as f:
f.read()