Question

I am searching and replacing string in a file and writing to it, using the function below

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)

And I call this

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)

But I get error -

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")

I checked and the process has write permissions to the directory where its writing to. Any ideas?

Était-ce utile?

La solution

I am not sure why but for some reason the file was not there, when the replace occur happened. I reduced it further and moved it to main. I also updated to search and replace using re module, rather than file input

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top