我正在學習Python,我是一個超級初學者! 我剛完成一項練習並想創建它的變體。 我的問題是,當使用IF/ELSE聲明時,我該如何避免使用「out_file = open(to_file,'w')out_file.write(indata)」兩次使用 。 在此先感謝!學習Python:導入存在
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)
in_file = open(from_file)
indata = in_file.read()
print "The input file is %d bytes long" % len(indata)
if exists(to_file):
print "File already exists, override?"
raw_input()
else:
out_file = open(to_file,'w')
out_file.write(indata)
out_file = open(to_file,'w')
out_file.write(indata)
print"Done."
out_file.close()
in_file.close()
我想你已經偶然發現了一個主要原因,爲什麼[函數](http://anh.cs.luc.edu/python/hands-on/3.1/ handsonHtml/functions.html#function-parameters)存在:) – jDo
正如我所說的,我剛開始深入編碼世界。所以,請給我一個例子,讓我可以學習! ;) –