在Python中重命名文件,获取FileNotFoundError

时间:2019-02-07 00:45:27

标签: python python-3.x

我不断收到此错误:

Traceback (most recent call last):
  File "/Users/ctostaine/Box/Junior Year/Spring 2019/INFO-I 211/ctostain_B1_indiv.py", line 32, in <module>
    os.rename(file, file.replace('draft', 'final'))
FileNotFoundError: [Errno 2] No such file or directory: 'blah1(draft).rtf' -> 'blah1(final).rtf'

这是我的代码:

import os

#retrieve directory from user
usr_direct = input('Please enter a directory: ')

#put the items in the directory in a list
direct_list = os.listdir(usr_direct)
print(direct_list) #// for testing

#iterate through list and change the items
for file in direct_list:
    if 'draft' in file:
        os.rename(file, file.replace('draft', 'final'))

关于如何解决此错误的任何想法?我陷入困境是因为我觉得这应该可行,但是我不知道是什么阻止它更改文件名。

0 个答案:

没有答案