【问题标题】:Python subprocess with arguments带参数的 Python 子进程
【发布时间】:2018-07-30 08:09:24
【问题描述】:

我使用 python 子进程运行 python 脚本test.py。我想发送两个字符串'John', 'Henry' 作为参数并从test.py 中读取参数值。我想要这样的东西

subprocess.call(['python3', 'test.py']) #add two names 'John', 'Henry' as argument

test.py

print(name1) #print John
print(name2) #print Henry

我该怎么做?

【问题讨论】:

  • 你有尝试过吗?鉴于您提供的信息,非常不清楚您是如何提出第一行但无法使其发挥作用的。

标签: python


【解决方案1】:

试试这个:

subprocess.call(['python3', 'test.py', 'John', 'Henry'])

并将 test.py 更改为如下所示:

import sys

print(sys.argv[1])
print(sys.argv[2])

【讨论】:

  • 您好,感谢您的回答。我可以传递一个大字符串(几千个单词)作为参数吗?我刚试过,但它给出的错误参数列表太长
  • “参数列表太长”可能不是 python 的问题,它的 bash:stackoverflow.com/a/29802900/9348376
猜你喜欢
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
  • 2013-12-09
  • 2012-10-31
  • 2013-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多