python - 使用参数启动 python shell

标签 python linux shell terminal

是否可以在没有文件的情况下将参数传递给 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/

相关文章:

python - 递归函数将任意函数应用于任意长度的 N 个数组,形成一个 N 维的锯齿状多维数组

php - PHP 的 SSH 包?

php - 无法包含远程服务器上的文件

c - 如何从密文中获取ciphertext_len以在c程序中的openssl EVP_中使用

java - 在 shell 脚本中解析 wmic 的输出

linux - 在 shell 脚本中计算平均值时如何处理缺失值?

python - Django:包含的 urlconf core.urls 中没有任何模式

python - python中强度函数的积分

Python pandas 如何在将键一直向下存储的同时进行分组

linux shell脚本 child 的问题