zoukankan      html  css  js  c++  java
  • 将二级目录下的文件合并成一个文件的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
    ]+",' ', 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])
  • 相关阅读:
    64位整数乘法
    HTML中常见问题汇总贴
    题解 牛客【「水」悠悠碧波】
    题解 CF1391B 【Fix You】
    四级-句子
    快速幂||取余运算
    最大子列和
    JvavScript中的函数与对象
    JavaScript中的流程控制语句
    冒泡排序,选择排序,插入排序,归并排序
  • 原文地址:https://www.cnblogs.com/hwf-73/p/6541648.html
Copyright © 2011-2022 走看看