exercise17 更多文件

源程序:
from sys import argv
from os.path import exists    ##從os.path模塊中導入exists命令

script from_file, to_file = argv
print "Coping from %s to %s" %(from_file, to_file)
in_file=open(from_file)
indata=in_file.read()       ##將from_file文件打開並讀入其中數據 儲存到indata變量中
print"The input file is %d bytes long" %len(indata)     ##indata長度
print "Does the output file exist? %r" %exists(to_file)     ##此時沒有將數據讀入to_file 所以不存在
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()   ##需要用戶鍵入什麼東西???
out_file=open(to_file, 'w')  ##將to_file數據傳入out_file
out_file.write(indata)   ##將indata中數據寫入out_file中
print "Alright, all done."
out_file.close()
in_file.close()



命令行輸入:
echo "This is a test file." > test.txt  ##利用echo建立一個test.txt文件 文件內容爲 This is a test file.

$ cat test.txt  ## cat
This is a test file.
$
$ python ex17.py test.txt new_file.txt
Copying from test.txt to new_file.txt
The input file is 21 bytes long
Does the output file exist? False
Ready, hit RETURN to continue, CTRL-C to abort.
Alright, all done.



echo命令是把echo後面的內容作爲字符串顯示出來
cat 命令的用途是打印文件到屏幕上
想看一個文件的內容是什麼,可以用cat,比如 cat/etc/reslov.conf,就顯示出文件reslov.conf裏具體的內容。
比如 echo hello,就顯示hello;
比如 echo /etc/reslov.conf ,會顯示/etc/reslov.conf

問題1:from_file & to_file 兩個變量是新定義的






發佈了38 篇原創文章 · 獲贊 1 · 訪問量 7634
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章