试图在Python中重命名文件,但会遇到错误
我正在尝试编写一个代码以将文件重命名为数字,但我一直遇到此错误 FILENOTFOUNDERROR:[WINERROR 3]系统无法找到指定的路径:'Crypto Hood#100.jpg' - > ''
这是我的代码
os.chdir ('C:\\Users\\win 10\\Downloads\\Crypto Hood')
for filename in os.listdir('.'):
if filename.startswith('Crypto Hood #'):
os.rename(filename, filename[100:])
I am trying to write a code to rename files to numbers but i keep getting this errorFileNotFoundError: [WinError 3] The system cannot find the path specified: 'Crypto Hood #100.jpg' -> ''
this is my code
os.chdir ('C:\\Users\\win 10\\Downloads\\Crypto Hood')
for filename in os.listdir('.'):
if filename.startswith('Crypto Hood #'):
os.rename(filename, filename[100:])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我们采取:
然后
new_filename
将是一个空字符串(''
),因为file> fileName
只有20个字符,并且您将第一个100个切片。我认为您可能对切片的工作原理感到困惑,因此请参阅文档: https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.htmlIf we take:
then
new_filename
would be an empty string (''
) sincefilename
only has 20 characters and you are slicing out the first 100. Empty strings are not valid filenames in Windows, so you get this error. I think you may be confused about how slicing works, so refer the doc: https://python-reference.readthedocs.io/en/latest/docs/brackets/slicing.html我怀疑您想这样做:
I suspect you wanted to do this: