2014-01-22 40 views
0

我正在尋找,並在文件替換字符串,並寫入到它,使用下面OSERROR中的FileInput模塊在python

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

我檢查並且該進程對其寫入的目錄具有寫入權限。有任何想法嗎?

+0

您的格式已關閉。你傳遞給replaceAll()的參數的值是什麼? – Smith

+0

更新了全部功能。 agentHome是一個已定義的變量 –

+0

這裏只是一個猜測,但是當你嘗試重命名時,你正在編輯的文件是關閉的嗎? –

回答

0

我不知道爲什麼,但由於某種原因文件不存在,當發生替換髮生。我進一步減少了它,並將它移到了主要位置。我也更新了搜索和替換使用重新模塊,而不是文件輸入