私はこれを使って、ファイル内のテキストodirx
を置き換え、別のファイルに書き出します。しかし、私はsm_template2.txt
の代わりに出力ファイル名をsm_0120.txt
にしたいと思います。ファイル全体を読み取らずに出力ファイル名を変更するにはどうすればよいですか?
replacements = {'odirx':'0120'}
with open('/dir/sm_template.txt') as infile, open('/dir/sm_template2.txt', 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)
どうすればいいですか?
それはで読んだ後、ファイル全体を書き出すことができますが、私は、ファイル全体
関連で読まずにこれをやってみたかった:私はOKAY AM replacing text in a file with Python
TRIAL
私の唯一の選択であればそれを読んで
replacements = {'odirx':'0120'}
with open('/dir/sm_template.txt') as infile, open('/dir/sm_template2.txt', 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
lines.append(line)
outfile = os.path.join(os.path.normpath(os.getcwd()+os.sep+"sm_"+os.sep+target+os.extsep+"txt"))
どうすればいいですか?
あなたはそれを –
を保存すると、ファイルを変更するための使用os.rename彼は私が正しく理解していれば – jmercouris
はい@jmercourisファイルの2つのコピーを望んでいます!私の編集「TRIAL」を見てください – maximusdooku