Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

Python批量修改文件,删除文件名称的中文

请问怎么样才能通过Python修改一个文件夹下面的所有文件,让文件夹下面的文件名称中文全都删除,只留下数字和英文字母。求大佬告知


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
import glob
import re
import os
# 获取test文件夹下所有文件
file_list = glob.glob("./test/*")

# 正则匹配含有中文的文件,并移除
for file in file_list:
    if re.search('[u4e00-u9fa5]',file):
        print("chinese named file is ",file)
        os.remove(file)

# 查看最后剩下的文件
new_file_list = glob.glob("./test/*")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...