如何使用 python 脚本重命名文件?
在我的克隆站点上,原型对象的文件字段中有数百个文件(pdf,doc,...)。导入过程中出现问题,所有文件名都丢失了。问题是,当有人想要打开文件时,由于缺少扩展名,浏览器并不总是建议使用查看器打开它。
用户必须保存文件并添加扩展名才能打开它。
我可以编写一个 python 脚本来根据文件类型重命名具有扩展名的所有文件吗?
谢谢。
On my plone site I have hundreds of files (pdf, doc, ...) in filefield of archetypes objects. Something went wrong during importation and all the filenames are missing. The problem is that when somebody wants to open the file, since the extension is missing, the browser doesn't always propose to open it with a viewer.
The user has to save the file and add an extension to open it.
Can I write a python script to rename all files with an extension depending on the filetype?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
http://plone.org/documentation/manual/plone-community -developer-documentation/content/rename
这里有你需要的一切:)
重要的部分是:parent.manage_renameObject(id, id + "-old")
你可以循环子对象正在执行的操作:
context 是放置此脚本的文件夹,即存放所有 pdf 的文件夹
http://plone.org/documentation/manual/plone-community-developer-documentation/content/rename
you've all you need here :)
The important part is this: parent.manage_renameObject(id, id + "-old")
you can loop over the subobjects doing:
context is the folder where you put this script, the folder where you've all your pdfs
标准库函数 os.rename(src, dst) 就可以解决这个问题。如果您知道扩展名应该是什么(例如所有文件都是 .pdf),这就是您所需要的。如果您有一堆没有扩展名的 .doc、.pdf、.jpg、.xls 文件,则需要使用 python-magic。
The standard library function
os.rename(src, dst)
will do the trick. That's all you need if you know what the extension should be (e.g. all the files are .pdf). If you have a mixed bag of .doc, .pdf, .jpg, .xls files with no extensions, you'll need to examine the file contents to determine the proper extension using something like python-magic.