この練習をしながら、私は問題に遭遇しました。Pythonでは、ファイルを開いて1行で読むことができますが、その後もファイルを閉じることができますか?
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print "Copying from %s to %s" % (from_file, to_file)
# we could do these two on one line too, how?
input = open(from_file)
indata = input.read()
print "The input file is %d bytes long" % len(indata)
print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()
output = open(to_file, 'w')
output.write(indata)
print "Alright, all done."
output.close()
input.close()
# we could do these two on one line too, how?
は私を混乱させます。これは私が望んだ道を行っ
indata = open(from_file).read()
が、それは削除するために私を必要とします:私が思い付くことができる唯一の答えはなかった入力変数がもう存在しない
input.close()
として。どうしたら、このクローズ操作を実行できますか?
どうすれば解決できますか?
..私は、彼はあなたが後何であったかと思い
すなわち
既存のコードの行の間にセミコロンを使用するには、あなたにみんなありがとう。気分がよくなりました。 @ Paul D. Waite私の質問を編集していただきありがとうございます。今ははるかに明確です。 – yoonsiようこそ。他の人が質問の要点を知ることはしばしば簡単です。おそらく –