[英]Rename filename python
I'm trying to rename filenames stored in a directory using python. 我正在尝试使用python重命名存储在目录中的文件名。 Here is my script :
这是我的脚本:
from pathlib import Path
path = Path("photos")
for file in path.glob("*.jpg"):
newname = file.replace('Photo/siteTHQSE/','')
file.rename(file, newname)
However when I execute it I'm told that the replace method is requires 2 arguments but 3 were given: 但是,当我执行它时,我被告知replace方法需要2个参数,但给出了3个:
TypeError: replace() takes 2 positional arguments but 3 were given
Any ideas? 有任何想法吗?
It seems like you want the replace on the name of the file instead of the file itself. 好像你想要的文件,而不是文件本身的名称替换。
Something like: newname = file.name.replace('Photo/siteTHQSE/','')
诸如此类:
newname = file.name.replace('Photo/siteTHQSE/','')
You're using .replace()
incorrectly. 您使用的
.replace()
错误。 file
is not a string; file
不是字符串; it's a PosixPath
object. 这是一个
PosixPath
对象。 Its replace
method actually renames the file, and only takes one argument. 它的
replace
方法实际上是重命名文件,并且只接受一个参数。
Using bashcommands 使用bashcommands
bashCommand = "cp oldfile newfile" os.system(bashCommand)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.