在python中的单独文件夹中重命名一组文件
大家好,在 python 中重命名单独文件夹中的一组文件时遇到问题。我有一个文件夹结构,就像
images/p_0/aaa.jpg,bbb.jpg
images/p_1/aaa.jpg,bbb.jpg
images/p_2/aaa.jpg,bbb.jpg
我需要将这些 jpg 重命名为喜欢的
images/p_0/A-1.jpg,B-1.jpg
images/p_1/A-2.jpg,B-2.jpg
images/p_2/A-3.jpg,B-3.jpg
,并且我正在使用 os.rename 方法。我只是不知道如何循环浏览文件夹并找到图像并重命名它。
Hi guys have a problem with renaming a set of files in separate folder in python. i have a folder structure like
images/p_0/aaa.jpg,bbb.jpg
images/p_1/aaa.jpg,bbb.jpg
images/p_2/aaa.jpg,bbb.jpg
I need to rename these jpg to like
images/p_0/A-1.jpg,B-1.jpg
images/p_1/A-2.jpg,B-2.jpg
images/p_2/A-3.jpg,B-3.jpg
and i am using the os.rename
method. I just don't get how to cycle through the folders and find the image and rename it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用 os.walk 和 os.rename。
Use os.walk and os.rename.
您可以使用 os.listdir() 来获取某个目录中的所有文件。您可能还想查看 os 模块 文档的其余部分。
You can use
os.listdir()
to get all the files in some directory. You might also want to look through the rest of the os module documentation.查看
os.listdir
或os.walk
,如果您有嵌套目录结构。Take a look at
os.listdir
oros.walk
, if you have a nested directory structure.除了 os.walk() 之外,glob.glob() 可能也有用。
In addition to os.walk(), glob.glob() might be useful.