python学习日记ex17
文章部分來自《笨辦法學python》
感謝作者提供這么好的書這一節的主要內容是將一個文件內z容拷貝到另外一個文件里面。
源代碼來自《笨辦法學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)
#we could do these two on one line,how?
#打開from_file文件,返回一個文件對象給in_file
in_file = open(from_file)
#讀取文件內容以字符串的形式賦給indate
indate = in_file.read()
#返回這個文件大小
print "The input file is %d bytes long" % len(indate)
#返回目標文件是否存在此路徑
print "Does the output file exist? %r " % exists(to_file)
#繼續腳本按回車,結束按CTRL-C
print "Ready,hit RETURN to continue , CTRL-C to abort."
raw_input()
#用寫的方式打開目標文件
out_file = open(to_file,'w')
#將字符寫進目標文件
out_file.write(indate)
print "Alright , all done."
in_file.close()
in_file.close()
總結
以上是生活随笔為你收集整理的python学习日记ex17的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Firefox OS 2.0 模拟器 –
- 下一篇: Python-电脑摄像头拍照,并生成图片