2011-11-17 140 views
1

我在使用Learn Python The Hard Way來學習Python。這是非常好的和有效的,但有一次我碰到了一個問題。我搜索了網頁,但找不到答案。 這裏是我的問題:使用argv時出錯

之一練習告訴做到這一點:

from sys import argv 

script, filename = argv 

然後前進到做的事情,我明白:

print "we are going to erase %r." % filename 
print "if you don't want that, hit CTRL-C (^C)." 
print "if you do want that, hit RETURN." 

raw_input("?") 

print "opening the file..." 
target = open(filename, 'w') 

什麼第一部分意思?

P.S.我得到的錯誤是:後

的SyntaxError意外的字符續行符

+1

請提供完整的錯誤信息 – skayred

回答

2
script, filename = argv 

這是unpacking the sequenceargv。第一個元素進入script,第二個元素進入filename。一般來說,只要左邊的變量和右邊的迭代項中的變量一樣多,就可以用任何迭代來完成。

您顯示的代碼似乎沒問題,我不知道爲什麼你在那裏得到語法錯誤。

+0

沒錯,唯一的問題是,用戶是錯誤的(我)。我將你在那裏所說的話解釋爲腳本中的內容。完全錯誤的方式。我必須把它寫在命令行上。非常感謝您的幫助。 –

1

Unexpected character after line continuation character意味着你已經在兩行使用繼續符\(見this question)劃分的命令之後卻增加了一些字符(例如白空間)。

但我沒有看到你的代碼中的任何\ ...

1

的代碼工作正常,把代碼在codefile.py的例子,並通過一個dummydata文件到它:

$ python codefile.py dummydatafile.txt 
We're going to erase 'test1.txt'. 
If you don't want that, hit CTRL-C (^C). 
If you do want that, hit RETURN. 
? 
Opening the file... 
Truncating the file. Goodbye! 
Now I'm going to ask you for three lines. 
line 1: 
line 2: 
line 3: 
I'm going to write these to the file. 
And finally, we close it. 
$ 

這應該解決您的問題