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

时间:2022-02-25 15:27:21

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

 #coding:utf8
import sys, os def process(path):
new_file = open("file_1", "a+")
for secDir in os.listdir(path):
for f in os.listdir(path + "/" + secDir):
fin = open(path + "/" + secDir + "/" + f, "r")
content = fin.readline()
while len(content) > 0:
new_file.write(content)
content = fin.readline()
fin.close()
new_file.close() if __name__ == "__main__":
process(sys.argv[1])

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

 #coding:utf8
import sys, os
import re def process(path):
new_file = open("file_2", "a+")
for secDir in os.listdir(path):
for f in os.listdir(path + "/" + secDir):
fin = open(path + "/" + secDir + "/" + f, "r")
content = fin.readline()
while len(content) > 0:
new_content = re.sub("[^a-zA-Z\r\n]+",' ', content)
new_file.write(new_content)
content = fin.readline()
fin.close()
new_file.close() if __name__ == "__main__":
process(sys.argv[1])