gpt4 book ai didi

python - 如何将文件夹中的所有文件类型更改为特定类型?

转载 作者:太空宇宙 更新时间:2023-11-04 05:42:59 24 4
gpt4 key购买 nike

这些文件包含不同的文件类型。在尝试使用 cmd 更改一堆文件名并使用此类技术后,这并没有改变。找到屏幕截图以便更好地理解。

enter image description here

我从这里使用了这样的Python代码,但它不起作用。 How to change multiple filenames in a directory using Python

最佳答案

您可以使用os来重命名,并使用glob来方便地获取文件列表:

import os, glob

def rename(files, pattern, replacement):
for pathname in glob.glob(files):
basename= os.path.basename(pathname)
new_filename= basename.replace(pattern, replacement)
if new_filename != basename:
os.rename(
pathname,
os.path.join(os.path.dirname(pathname), new_filename))

rename('*.XXX', 'XXX', 'YYY')

灵感来自this answer

关于python - 如何将文件夹中的所有文件类型更改为特定类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41283195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com