我有一個簡單的實用程序腳本,用於下載給定URL的文件。它基本上只是一個Linux二進制「aria2c」的包裝。引用的字符串作爲Python腳本的可選參數
這裏是名爲getFile
腳本:
#!/usr/bin/python
#
# SCRIPT NAME: getFile
# PURPOSE: Download a file using up to 20 concurrent connections.
#
import os
import sys
import re
import subprocess
try:
fileToGet = sys.argv[1]
if os.path.exists(fileToGet) and not os.path.exists(fileToGet+'.aria2'):
print 'Skipping already-retrieved file: ' + fileToGet
else:
print 'Downloading file: ' + fileToGet
subprocess.Popen(["aria2c-1.8.0", "-s", "20", str(fileToGet), "--check-certificate=false"]).wait() # SSL
except IndexError:
print 'You must enter a URI.'
因此,例如,該命令將下載文件:
我想要做的就是允許一個可選的第二個參數(後$ getFile http://upload.wikimedia.org/wikipedia/commons/8/8e/Self-portrait_with_Felt_Hat_by_Vincent_van_Gogh.jpg
該URI)是一個帶引號的字符串。該字符串將是下載文件的新文件名。所以,下載完成後,文件將根據第二個參數重命名。使用上面的例子,我希望能夠進入:
$ getFile http://upload.wikimedia.org/wikipedia/commons/8/8e/Self-portrait_with_Felt_Hat_by_Vincent_van_Gogh.jpg "van-Gogh-painting.jpg"
但我不知道如何把一個帶引號的字符串作爲可選參數。我怎樣才能做到這一點?
哦,所以如果你將一個帶引號的字符串傳遞給Python,它會自動將它作爲一個單一的參數? – synaptik
正確;引用是一個shell特性,與Python無關,真的。 –
啊,好的,謝謝。我想我沒有對這個問題進行盡職調查。我至少應該嘗試一下,看看論證是如何工作的,而不是假設它不會像你描述的那樣工作。 :)所以,請原諒我的懶惰 - 非常感謝! – synaptik