我的腳本打開一個文件(a.txt)並將其複製到一個新文件(b.txt)。如果新文件存在,它將索引名稱(b1.txt),以免覆蓋現有文件。現在我想在函數外使用最新的名稱(b#.txt)(方法? - 對不起,我是新手)。在方法中訪問局部變量?
這是我的腳本的一部分。當我複製原始文件後,我調用replaceAll方法替換某些文本時,我想使用newFile的當前值。
class classMain:
def copyFile(self,origFile,newFile):
fIn = open(origFile,'r')
i = 0
while os.path.exists(newFile):
i += 1
newFile = 'b'+str(i)+'.txt'
fOut = open(newFile,'w')
for line in fIn:
fOut.writelines(line)
fIn.close()
fOut.close()
def replaceAll(self,file,searchExp,replaceExp):
for line in self.fIn(file, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
謝謝!
你爲什麼要在課堂上包裝這個? – 2012-03-17 22:22:54