zoukankan      html  css  js  c++  java
  • python 文件夹内容与txt文件转换

     1 #!/usr/bin/python3
     2 # 文件名:ProjectTransfer.py
     3 
     4 import os
     5 class ProjectTransfer:
     6     def __init__(self, txtFilePath='D:/src.txt', srcPath='D:/mhi', srcPathTo='D:/mhi2', contentTypes=('.java','.xml','.jsp','.properties'), nameTypes=('.js','.css')):
     7         self.txtFilePath = txtFilePath
     8         self.srcPath = srcPath
     9         self.contentTypes = contentTypes
    10         self.nameTypes = nameTypes
    11         self.srcPathTo = srcPathTo
    12     
    13     def file2Txt(self, filePath, txtf):   
    14         files = os.listdir(filePath) 
    15         for f in files:
    16             cf = filePath + '/' + f
    17             if not os.path.isdir(cf):
    18                 if os.path.splitext(f)[1] in self.contentTypes:
    19                     txtf.write('#BeginFile:' + f + '
    ')
    20                     txtf.write('#FilePath:' + cf + '
    ')
    21                     with open(cf, encoding='utf-8') as inputf:
    22                         txtf.writelines(iter(inputf))
    23                     txtf.write('
    #EndFile:' + f + '
    ')
    24                 elif os.path.splitext(f)[1] in self.nameTypes :
    25                     txtf.write('#BeginFile:' + f + '
    ')
    26                     txtf.write('#FilePath:' + cf + '
    ')
    27                     txtf.write('#EndFile:' + f + '
    ')
    28             else:
    29                 self.file2Txt(cf, txtf)
    30                 
    31     def txt2File(self):
    32         f = None
    33         with open(self.txtFilePath, 'r', encoding='utf-8') as txtf:
    34             for line in txtf:
    35                 if line.startswith('#BeginFile:'):
    36                     pass
    37                 elif line.startswith('#FilePath:'):
    38                     filePath = line[10:].replace(self.srcPath, self.srcPathTo)[:-1]
    39                     if(f and not f.closed):
    40                         f.close()
    41                     dirname=os.path.dirname(filePath)
    42                     if not os.path.exists(dirname):
    43                         os.makedirs(dirname)
    44                     f = open(filePath, 'w', encoding='utf-8')
    45                 elif line.startswith('#EndFile:'):
    46                     f.close()
    47                 else:
    48                     f.write(line)
    49         
    50                 
    51 trans = ProjectTransfer()
    52 if os.path.exists(trans.txtFilePath):
    53     os.remove(trans.txtFilePath)
    54 with open(trans.txtFilePath, 'a', encoding='utf-8') as txtf:
    55     trans.file2Txt(trans.srcPath, txtf)
    56     
    57 trans.txt2File()
  • 相关阅读:
    Vue的响应式和双向绑定的实现
    JS-跨域请求豆瓣API提供的数据
    豆瓣电影API接口
    JS/PHP-表单文件域上传文件和PHP后台处理
    jQuery-attr,prop及自定义属性
    PHP-关于php代码和html,js混编
    JS-Chrome控制台console.log会访问即时数据
    JS-time和timeEnd
    JS-用ID值直接操作DOM
    CSS-07 行内设置点击事件
  • 原文地址:https://www.cnblogs.com/xuemanjiangnan/p/8710119.html
Copyright © 2011-2022 走看看