我正在处理一个项目,该项目允许用户通过添加必要的参数设置上传文件的路径,但由于某些原因,upload_destination变量总是为空!下面是我的代码:
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)? - linusgs =“something/new”
,则最终会变成s =“something(换行符)ew”
。 - Peterelif
代码块被执行了吗?如果在upload_destination = a
前一行加上print("about to change upload destination")
,它会被打印出来吗? - Kevinmain()
函数? - cdarke