書中**:
from sys import ar**
from os.path import exists
script, from_file, to_file = ar**
print "copying from %s to %s" % (from_file, to_file)
# we could do these two on one line, how?
in_file = open(from_file)
indata = in_file.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()
out_file = open(to_file, 'w')
out_file.write(indata)
print "alright, all done."
out_file.close()
in_file.close()
**修改為
from sys import ar**
from os.path import exists
script, from_file, to_file = ar**
print("copying from %s to %s " % (from_file, to_file))
# we could do these two on one line , how?
in_file = open(from_file)
indata = in_file.read()
print("the input file is %d bytes long" % len(indata))
print("dose the output file exist? %r" % exists(to_file))
print("ready, hit return to continue, ctrl-c to abort.")
input()
out_file = open(to_file, 'w')
out_file.write(indata)
print("alright, all done.")
out_file.close()
in_file.close()
輸出結果:
執行該指令碼需要兩個引數,乙個是待拷貝的檔案,乙個是要拷貝至的檔案。我再建立乙個名為 test.txt 的測試檔案,我們將看到如下的結果:
echo "this is a test file." > test.txt
執行指令碼拷貝檔案
python系統學習日記 L16 讀寫檔案
下面的命令,是你應該記住的命令 close 關閉檔案。跟你編輯器的 檔案 儲存 乙個意思。read 讀取檔案內容。你可以把結果賦給乙個變數。readline 讀取文字檔案中的一行。truncate 清空檔案,請謹慎使用該命令。write stuff 將stuff寫入文 書中 from sys imp...
Python系統學習 02
資料型別 數值用於計算,布林值用於條件執行等等。例子 s i m a student 錯誤,也可以在 前面新增轉義字元 s i m a student s1 morgan s2 xu print s1 s2 morgan xu s1 morgan print s1 8 morgan morgan m...
Python系統學習 07
一種規範,寫 時的規範 from abc import abcmeta,abstractmethod class istream metaclass abcmeta abstractmethod defread self,data print read super method abstractme...