Python3 检查并去除文件空格

 1 #!/usr/bin/python3
 2 #coding=utf-8
 3 
 4 import sys
 5 import os
 6 import re
 7 
 8 def renamefile(name1,name2):
 9     os.rename(name1,name2)
10 
11 def checkfilelegal(path):
12     if re.compile(r'\s').findall(path):
13         file.write(path + '\n')
14         return True
15         
16     return False
17         
18 def checkfile(path):
19     for file in os.listdir(path):
20         file_path = os.path.join(path,file)
21         if True == checkfilelegal(file_path):
22             renamefile(file_path,re.sub(r'\s','',file_path))
23         if os.path.isdir(file_path):
24             checkfile(file_path)
25 
26 if __name__ == '__main__':
27     filepath = input('input checkfile path:')
28     file = open('log.txt','w',encoding='utf-8')
29     checkfile(filepath)
30     file.close()
31      

使用缺陷,执行脚本的时候要加上 python3,例如 python3 *.py,不能直接拖入执行。望指点!非常感谢

posted @ 2021-05-24 19:28  天星铁甲堂  阅读(220)  评论(0)    收藏  举报