我正在使用 Python3,每次运行下面的代码 Python3 ex14.py
时,我都会打印 (f"Hi {user_name}, I'm the {script } script.")
我得到了 {user_name}
正确的结果,但是 {script}
显示了我正在运行的文件以及变量 {user_name }
from sys import argv
script = argv
prompt = '> '
# If I use input for the user_name, when running the var script it will show the file script plus user_name
print("Hello Master. Please tell me your name: ")
user_name = input(prompt)
print(f"Hi {user_name}, I'm the {script} script.")
如何只打印正在运行的文件?
最佳答案
argv
收集所有命令行参数,包括脚本本身的名称。如果您想排除该名称,请使用argv[1:]
。如果您只需要文件名,请使用argv[0]
。在您的情况下:script = argv[0]
。
关于python - 在Python3中使用Script = argv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51040077/