Mat*_*ley 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()
Run Code Online (Sandbox Code Playgroud)
然后称之为:
./foo.py bar.txt baz.txt
Map*_*pad 12
我想你错过的是如何检索该目录中的所有文件.为此,请使用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()
Run Code Online (Sandbox Code Playgroud)