文件A的內容復制到B
以下腳本摘自笨學python,個人添加了備註
------------------------------------------------------------
1、腳本
from sys import argv
from os.path import exists
script,from_file,to_file = argv
print("Copy file %s to file %s" %(from_file,to_file))
#以讀的方式打開from_file
in_file = open(from_file,"r")
#將in_file文檔的內容讀取出來
indata = in_file.read()
#將in_file文檔的內容打印出來
print(indata)
print("the input file is %d bytes long" %len(indata))
print("Does the output file exists? %r" %exists(to_file))
print ("ready,hit RETURN to continue, CTRL-C to abort.")
#以讀的方式打開to_file
out_file = open (to_file,‘w‘)
#將indata的內容寫入out_file
out_file.write(indata)
#在寫入後,再次以讀的方式打開to_file
out_file = open(to_file,‘r‘)
#將out_file文檔的內容讀取出來
outdata = out_file.read()
#將out_file文檔的內容打印出來
print(outdata)
out_file.close()
in_file.close()
print("Alright,all done.")
2、執行結果
3、01txt內容為:
文件A的內容復制到B