这个小程序的目的是将二级目录下的文件全部合并成一个文件(其实几级目录都可以,只要做少许改动)
- 1 #coding:utf8
- 2 import sys, os
- 3
- 4 def process(path):
- 5 new_file = open("file_1", "a+")
- 6 for secDir in os.listdir(path):
- 7 for f in os.listdir(path + "/" + secDir):
- 8 fin = open(path + "/" + secDir + "/" + f, "r")
- 9 content = fin.readline()
- 10 while len(content) > 0:
- 11 new_file.write(content)
- 12 content = fin.readline()
- 13 fin.close()
- 14 new_file.close()
- 15
- 16 if __name__ == "__main__":
- 17 process(sys.argv[1])
来源: http://www.bubuko.com/infodetail-1980401.html