1 import os 2 3 # 遍歷目錄下的所有文件 4 def check_file(file_path): 5 os.chdir(file_path) 6 print(os.path.abspath(os.curdir)) 7 all_file = os.listdir() 8 files = [] 9 for f in all_file: 10 if os.path.isdir(f): 11 files.extend(check_file(file_path+'\\'+f)) 12 os.chdir(file_path) 13 else: 14 # files.append(f) 15 files.append(os.path.abspath(f)) 16 return files 17 18 19 # 刪除前N行 20 def delete_line(file_path): 21 with open(file_path, 'r', encoding='utf-8') as f: 22 lines = f.readlines()[15:] 23 24 with open(file_path, 'w', encoding='utf-8') as f: 25 for line in lines: 26 f.write(line) 27 28 29 file_list = check_file("D:/workspace/soas-console/src/main/java/com/soa/supervision/admin") 30 for i in file_list: 31 print(i) 32 delete_line(i)
Python遞歸遍歷目錄並刪除文件中的前N行
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.