import os def get_all_path(path='./'): l = os.walk(path) file_dir = [] for i, v, files in l: for file in files: file_dir.append(os.path.join(path, file)) return file_dir
批量改写文件
def get_all_path(path='.'): l = os.walk(path) file_dir = [] for i, v, files in l: for file in files: if file.split('.')[0] == 'test' or file.split('.')[0] == '1': print(file.split('.')[0]) with open(file, 'r+') as f: content = f.readlines() content[7] += '<h1>' content[8] += '</h1><div style="text-align: right">' content[9] += '</div><p style=" text-indent:2em">' content[10] += '</p>' content = ''.join(i for i in content) with open(file, 'w+') as f: f.write(content) return 'ok' get_all_path()