<p>我的脚本打开一个文件(a.txt)并将其复制到一个新文件(b.txt)。如果新文件存在,它会索引名称(b1.txt),所以我不会覆盖现有的文件。现在我想在函数(method)之外为复制的文件(b#.txt)使用最新的名称?-抱歉,我是新来的)。你知道吗</p>
<p>这是我剧本的一部分。在复制原始文件后调用replaceAll方法替换某些文本时,我希望使用newFile的当前值。你知道吗</p>
<hr/>
<pre><code>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)
</code></pre>
<p>谢谢!你知道吗</p>