一个小需求:
在申请软件著作权的时候,需要提交一页50行,总共60页的源代码。但是设计的项目保存在多级的目录下,不想一个一个复制,遂通过python ,os模块获得全部目录的文件,re正则化过滤无效源代码,然后基于docx模块写入到word中。涉及的模块有 os, docx, re
如果您可能没有上述模块的话,
使用 pip install XX 进行下载
分为2 个大的步骤:
1. 先将一个文件夹下的所有文件夹的 .java 文件路径保存到一个列表中
2. 依次读取列表的路径, 将 .java 文件内容保存到word 中
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__title__ = ''
__author__ = 'mike_jun'
__mtime__ = '2019-7-1'
#目的: 1. 先将一个文件夹下的所有文件夹的 .java 文件路径保存到一个列表中
2. 依次读取列表的路径, 将 .java 文件内容保存到word 中
"""
import os
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt
import re
from docx.shared import Length
fileList = [] # 使用全局列表保存文件路径
def getAllFile(path, fileList): # 使用递归方法
dirList = [] # 保存文件夹
files = os.listdir(path) # 返回一个列表,其中包含文件 和 文件夹
for f in files:
if (