将二级目录下的文件合并成一个文件的Python小脚本

这个小程序的目的是将二级目录下的文件全部合并成一个文件(其实几级目录都可以,只要做少许改动)

 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])

 将这个程序稍作修改,可以实现只留下文件中的字母,去除其他字符的功能:

 1 #coding:utf8
 2 import sys, os
 3 import re
 4 
 5 def process(path):
 6     new_file = open("file_2", "a+")
 7     for secDir in os.listdir(path):
 8         for f in os.listdir(path + "/" + secDir):
 9             fin = open(path + "/" + secDir + "/" + f, "r")
10             content = fin.readline()
11             while len(content) > 0:
12                 new_content = re.sub("[^a-zA-Z\r\n]+",' ', content)
13                 new_file.write(new_content)
14                 content = fin.readline()
15             fin.close()
16     new_file.close()
17 
18 if __name__ == "__main__":
19     process(sys.argv[1])

 

posted @ 2017-03-13 10:49  阿基米德的鸭子  阅读(402)  评论(0编辑  收藏  举报