簡體   English   中英

如何進一步縮短將一個文件的內容復制到另一個文件的Python腳本?

[英]How can I further shorten this Python script which copies the contents of one file to another file?

我正在閱讀Zed Shaw的“學習Python的艱難方法”。 我要練習17( http://learnpythonthehardway.org/book/ex17.html ),並在第2和3號代碼上加分。Zed希望我通過消除所有不必要的內容來縮短腳本必要的(他聲稱他可以讓它只在腳本中運行一行)。

這是原始腳本...

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 too, how?
input = open(from_file)
indata = input.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()

output = open(to_file, 'w')
output.write(indata)

print "Alright, all done."

output.close()
input.close()

這就是我能夠將腳本縮短為並且仍然可以使其正常運行的功能(通過正確的意思是腳本成功地將了預期的文本復制到了預期的文件中)...

from sys import argv
from os.path import exists

script, from_file, to_file = argv

input = open (from_file)
indata = input.read ()

output = open (to_file, 'w')
output.write (indata)

我擺脫了打印命令和兩個關閉命令(請原諒,如果我不正確地使用“命令”……我是編碼的新手,還沒有把專業術語講清楚)。

我試圖進一步縮短腳本的任何其他操作都會產生錯誤。 例如,我嘗試將“ input”和“ indata”命令組合成一行,如下所示:

input = open (from_file, 'r')

然后,我將腳本中的所有“ indata”引用都更改為“ input”。

from sys import argv
from os.path import exists

script, from_file, to_file = argv

input = open (from_file, 'r')

output = open (to_file, 'w')
output.write (input)

但是我收到以下TypeError ...

new-host:python Eddie$ python ex17.py text.txt copied.txt
Traceback (most recent call last):
  File "ex17.py", line 10, in <module>
    output.write (input)
TypeError: expected a character buffer object

您將如何進一步縮短腳本……或像Zed所建議的那樣將其縮短到僅一行?

您可以只使用shutil庫,而讓OS承擔復制的負擔(而不是用Python讀取/寫入數據)。

import shutil
shutil.copy('from_file', 'to_file_or_directory_name')

您得到的當前錯誤是由於以下原因:

input = open (from_file, 'r')

output.write (input)

write()需要一個字符串作為參數,您正在給它一個文件對象。

另外,由於您要消除多余的內容/縮短代碼(小的內容),因此打開文件的默認模式為'r' r'ead,因為打開文件進行讀取時無需指定該模式。

還可以考慮使用with構造來打開和管理文件。 這樣做的好處是,完成操作或遇到異常后,文件將自動為您關閉,因此不需要顯式的close() 例如,

with open('data.txt') as input:
   ## all of your file ops here

PEP08-Python樣式指南 (Python程序員的“必讀”)建議在函數和開頭之間不要空格(

我不確定單行目標是否總會帶來更好或更可讀的解決方案,因此請牢記這一點。

from sys import argv
open(argv[2], 'w').write(open(argv[1]).read())

差不多就可以了。 您可以使用分號將它們連接到一行中,但這只是用其他內容代替行尾字符而沒有真正的用處。

不確定這是否是作者想要的東西,但是這是經過多次試驗和運行后我想到的解決方案。 我本人是一個初學者,所以請記住這一點。 這是腳本:

               from sys import argv; script, from_file, to_file = argv
               in_file = open(from_file).read(); out_file = open(to_file, 'w').write(in_file)

我從字面上理解了“ in one line”指令,並完全省略了導入行。 我也覺得它更友好:

open(raw_input("To file? "), "w").write(open(raw_input("From file? ")).read())

仍然沒有關閉“要歸檔”,但嘿,一行!

-編輯-我剛剛注意到,腳本完成后,Python 確實關閉了“ To file”。 所以,是的,一行!

接下來呢? 以為這樣可以。

open(input(),'w').write(open(input()).read())

這是我想出的(也是初學者):

    from sys import argv

    script, from_file, to_file = argv

    open(to_file, 'w').write(from_file)

它可以工作,但是我不確定是否打開了任何文件。 我想我可能仍然需要close()命令。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM