os.rename不在python中工作

时间:2017-03-06 21:57:21

标签: python

当我在python代码下面执行时出现错误:

    #!/comm/python/stable/bin/python

import re
import os
import shutil

DestDir="/home/SVModels/"
vamsList="/home/vamsList.txt"

with open(vamsList) as f:
    for line in f:
        fileObj= re.split (r'(/)', line)
        OldfileName=fileObj[-1]
        NewfileName=".".join([fileObj[14], "sv"])
        shutil.copy(line.rstrip(), DestDir)
        print("OldfileName= {}NewfileName= {}".format(OldfileName, NewfileName))
        os.chdir(DestDir)
        print "Current working dir : %s" % os.getcwd()
        os.rename(OldfileName,NewfileName)

这是错误:

Traceback (most recent call last):
  File "/home/ConvertVamsToSV.py", line 20, in <module>
    os.rename(OldfileName,NewfileName)
OSError: [Errno 2] No such file or directory

Process finished with exit code 1

看起来谎言os.rename无法替换&#34; OldfileName&#34;用字符串。当我实际上将os.rename命令更改为硬引用&#34; OldfileName&#34;然后它工作。 例如os.rename(&#34; myfile.vams&#34;,NewfileName)

知道为什么原始代码无效吗?

0 个答案:

没有答案