-
python 批量更改文编码
import os # 更改文件编码 def recoding(filename): try: with open(filename, 'r', encoding = 'gbk') as fr: doc = fr.read() with open(filename, 'w', encoding = 'utf-8') as fw: fw.write(doc) except UnicodeDecodeError: print(filename + " 不是 GBK 编码") # 扫描指定目录及子目录下所有文件 def fscan(path, filepaths): for root, dirs, files in os.walk(path): # 获取目录下所有文件,文件夹信息 for file in files: # 遍历文件信息 if '.cpp' in file: # 如果是 .cpp 文件 filepath = os.path.join(root, file) # 添加完整路径 filepaths.append(filepath) if __name__ == "__main__": filepaths = [] # 获取目录下所有文件 fscan("D:\\Hello", filepaths) # 更改文件编码 for file in filepaths: recoding(file)
-
python 批量更改文件名
import os if __name__ == "__main__": files = os.listdir("./") for file in files: if ".jpg" in file: name, type = file.split(".") os.rename(file, name + ".png")
网友评论