gpt4 book ai didi

python - 使用参数启动 python shell

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:19 24 4
gpt4 key购买 nike

是否可以在没有文件的情况下将参数传递给 linux 中的 python?我目前无法创建文件或更改权限,我不想像这样在我的代码中编写它:

import sys
sys.argv = ["arg1", "arg2", ...]

我想在启动 shell 时交出参数:

python <arguments>

最佳答案

虽然将命令行参数传递给交互式 shell 是否是最佳实践值得怀疑,但确实可以通过传递 - 而不是脚本文件名来实现:

$ python - a1 a2
Python 2.7.14 (default, Sep 23 2017, 22:06:14)
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.argv
['-', 'a1', 'a2']

关于python - 使用参数启动 python shell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48059700/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com