用新文件名替换 python 中的文件名
可能的重复:
在 Python 中重命名文件
大家好,我正在用 Python 创建一个脚本来包装多个操作系统与批量图像编辑相关的命令,并且大多数任务都已完成,但是我被一项任务(看似简单的任务)难住了。
当我在扫描仪上扫描图片时,我得到一个与此类似的文件名:
201105151110_0001A.jpg
,其中第一部分是某种时间戳(我想替换它),但是我想将其作为输入变量(其中我,或者其他用户可以将其粘贴到命令行中,如果我使用另一个扫描仪,其中文件名结构可能不同,0001A 表示第一张照片/文档的正面,我想保留文件名的这一部分。 working_directory
我设置了三个变量:
old_prefix = input(bcolors.PROMPT + "Enter the prefix to replace: " + bcolors.ENDC)
new_prefix = input(bcolors.PROMPT + "Enter the new prefix: " + bcolors.ENDC)
working_directory
是代码另一部分的变量,它是图像所在的位置,这样我就可以对输出进行着色并使其更易于阅读。当我处理该脚本时,
该脚本将在目录中运行,
谢谢!
---编辑---
抱歉浪费了你们的时间,我似乎忽略了此处链接的问题中的一些信息。作者:Kiril Kirov,我想出的有效代码是:
elif retouch_option == "06":
print(" ")
old_prefix = input(bcolors.PROMPT + "Enter the prefix to replace: " + bcolors.ENDC)
new_prefix = input(bcolors.PROMPT + "Enter the new prefix.......: " + bcolors.ENDC)
print(bcolors.OUTPUT + " ")
for fname in glob(working_directory + "*.jpg"):
keeper = fname[-9:]
print("Renaming image", keeper)
os.rename(fname, fname.replace(old_prefix, new_prefix))
我认为它应该是安全的,因为它只是用 new_prefix 变量替换 old_prefix 变量。这是真的吗?如果没有,我绝对会感谢反馈,尽管到目前为止这似乎运行良好。
Possible Duplicate:
Rename Files in Python
Hey all, I am creating a script in Python to wrap a number of OS commands related to bulk image editing, and most of the tasks are completed, however I am stumped on one task (a seemingly simple one at that).
When I scan pictures on my scanner, I get a filename similar to this:
201105151110_0001A.jpg
where the first part is some sort of a timestamp (which I want to replace), however I wanted to make that an input variable (where I, or other users could just paste that into the command line, if I'm using another scanner where the filename structure could be different. 0001A means the front side of the first photo/document, and I would like to keep that part of the filename.
I have three variables set up:
old_prefix = input(bcolors.PROMPT + "Enter the prefix to replace: " + bcolors.ENDC)
new_prefix = input(bcolors.PROMPT + "Enter the new prefix: " + bcolors.ENDC)
working_directory
working_directory is the variable from another part of the code, which is where the images would be located. The color part is just so I can colorize the output and make it easier to read. There would be anywhere from 1-1000 files in the directory when I work on it.
This script will be run on Linux.
Thank you!
---EDIT---
Sorry for wasting your time guys, it seems I overlooked some information in the question linked here by Kiril Kirov, the code that I came up with, which works is:
elif retouch_option == "06":
print(" ")
old_prefix = input(bcolors.PROMPT + "Enter the prefix to replace: " + bcolors.ENDC)
new_prefix = input(bcolors.PROMPT + "Enter the new prefix.......: " + bcolors.ENDC)
print(bcolors.OUTPUT + " ")
for fname in glob(working_directory + "*.jpg"):
keeper = fname[-9:]
print("Renaming image", keeper)
os.rename(fname, fname.replace(old_prefix, new_prefix))
I think it should be safe, since it is just replacing the old_prefix variable with the new_prefix variable. Is this true? If not I'd definitely appreciate feedback, although this seems to be working fine so far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的东西:
something like: