python - Using Script = argv in Python3
问题描述
I'm playing around with Python3 and every time I run the code Python3 ex14.py
below and I print the (f"Hi {user_name}, I'm the {script} script.")
I get the {user_name}
right but the {script}
shows the file I'm running plus the variable {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.")
How can I print just the file I'm running?
解决方案
argv
收集所有命令行参数,包括脚本本身的名称。如果要排除名称,请使用argv[1:]
. 如果您只想要文件名,请使用argv[0]
. 在你的情况下:script = argv[0]
。
推荐阅读
- node.js - req.params.id 未定义,ReferencesError : id 未定义
- sql - 如何查看值子集中的每个值至少出现一次?
- javascript - 如何根据一个等值的性质得到数组中各种对象的正负值之和?
- python - 具有大可迭代的多处理
- aws-lambda - 来自 Lambda 函数的 AssumeRole 间歇性成功
- debugging - gdb 不透明符号解析已关闭
- java - 按键对对象进行分组并创建树
- javascript - 如何使用 Dynamoose 正确查询索引
- spring-boot - Spring Webflux 上传大图像文件并使用 WebClient 以流的方式发送文件
- r - BiodiversityR::rankabundance() 函数不计算 plower 和 pupper 值