我正在研究一個項目,該項目允許用戶通過添加必要參數來設置上傳文件的路徑,但無論出於何種原因,upload_destination變量始終爲空! 這裏是我的代碼Python獲取參數
def main():
global listen
global port
global execute
global command
global upload_destination
global target
if not len(sys.argv[1:]):
usage()
try:
opts, args = getopt.getopt(sys.argv[1:], "hle:t:p:cu", ["help", "listen", "execute", "target", "port", "command", "upload"])
except getopt.GetoptError as err:
print str(err)
usage()
for o,a in opts:
if o in ("-h", "--help"):
usage()
elif o in ("-l", "--listen"):
listen = True
elif o in ("-e", "--execute"):
execute = True
elif o in ("-c", "--commandshell"):
command = True
elif o in ("-u", "--upload"):
#Here's the problem, a is empty even though I include a path
upload_destination = a
elif o in ("-t", "--target"):
target = a
elif o in ("-p", "--port"):
port = int(a)
else:
assert False, "Unhandled Option"
if not listen and len(target) and port > 0:
buffer = sys.stdin.read()
client_sender(buffer)
if listen:
server_loop()
我輸入
C:\Users\Asus\Desktop\PythonTest>python test.py -l -c -p 3500 -u C:\Users\Asus\Desktop\Test
爲什麼不使用'argparse'(https://docs.python.org/3/library/argparse.html)? – linusg
可能根本不相關,但請嘗試使用正斜槓代替。就像如果你有's =「something/new」',它最終會成爲's =「something(newline)ew」' – Peter
@Peter我也想到了這一點,所以我試着簡單地輸入Test和它仍然是空的 – Aginu