将py文件打包到docx

import os


class FileDownload:
    def __init__(self):
        self.exclude = ['db.sqlite3', 'logs', 'media', 'Pipfile', 'Pipfile.lock', 'Readme.md']  # 不需要打包的文件
        self.file_list = []

    def get_file(self, path):

        if os.path.isfile(path):
            self.file_list.append(path)

        elif os.path.isdir(path):
            for file in os.listdir(path):

                if file not in self.exclude and not file.startswith('__') and not file.startswith('.'):
                    new_dir = os.path.join(path, file)
                    if not os.path.isdir(new_dir):
                        self.file_list.append(new_dir)
                    else:
                        self.get_file(new_dir)


if __name__ == '__main__':
    filepath = r'C:\Users\zhang\PycharmProjects\server'   # 项目所在目录
    file_obj = FileDownload()
    file_obj.get_file(filepath)
    file_list = file_obj.file_list  # 每一个文件的绝对路径
    with open('code.txt', encoding='utf-8', mode='w+') as w:  # 最终需要写入的文件:code.txt

        for file in file_list:  # 循环文件列表,对文件进行读操作
            with open(file, encoding='utf-8', mode='r') as f:
                for line in f.readlines():
                    w.write(line)

ip转int与int转ip

 

posted @ 2019-09-16 15:35  前方、有光  阅读(244)  评论(0编辑  收藏  举报