Python中os.rename不起作用

2024-09-30 12:29:14 发布

您现在位置: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)

错误如下:

^{pr2}$

看起来是假的操作系统重命名无法用字符串替换“OldfileName”。当我真的改变操作系统重命名命令硬引用“OldfileName”,然后它就可以工作了。 例如。操作系统重命名("我的文件.vams“,新文件名)

你知道为什么原来的代码不起作用吗?在


Tags: 代码importrehomeosline重命名comm

热门问题