python中fileinput模块中的OSError

时间:2014-01-22 19:47:30

标签: python jython

我正在搜索并替换文件中的字符串并使用下面的函数写入

def replaceAll(myfile,searchExp,replaceExp):
print "\nUpdating %s : Replacing %s with %s" %(myfile,searchExp,replaceExp)
for line in fileinput.input(myfile, inplace=1):
    if searchExp in line:
        line = line.replace(searchExp,replaceExp)
    sys.stdout.write(line)

我称之为

def downloadFiles():
for myfile in fileList.split(','):
    davFilePath = "%s/%s" %(webDavPath,myfile)
    targetFilePath = "%s/%s" %(emcliHome,myfile)
    getDavFiles(dav_user,dav_password,dav_url_base,davFilePath,targetFilePath)
            print "file is %s" %(myfile)
            if myfile == 'set_emcli.env':
                replaceAll(myfile,'AGENT_HOME_TEMPLATE',agentHome)

但我收到错误 -

for line in fileinput.input(myfile, inplace=1):
File "/opt/hp/dma/client/jython/Lib/fileinput.py", line 253, in next
line = self.readline()
File "/opt/hp/dma/client/jython/Lib/fileinput.py", line 322, in readline
os.rename(self._filename, self._backupfilename)
OSError: (0, "Couldn't rename file")

我检查过,该进程对其写入的目录具有写权限。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我不确定为什么但是由于某种原因文件不在那里,当发生替换时。我进一步减少它并将其移至main。我还更新了使用re模块搜索和替换,而不是文件输入