将路径变量传递给Python的子过程
我是子处理的新手,我有一个问题。 我在网上找不到适当的解决方案。
我希望我的道路进入变量 - >那将传递给功能 - >那将传递给子过程。
我不允许显示我的真实代码,但是这个简单的例子(我无法上班)会对我有很大帮助。
此代码段应该要做:
- 只需从变量中走我的路即可。
- “ CD” CMD中的路径。
- 打开位于此路径中的文件。
到目前为止我尝试过:
import subprocess
test_path = "C:/randome_path/.."
def Test_Function(test_path):
subprocess.call("cd", test_path, shell = True)
subprocess.call("python file.py", shell = True)
Test_Function()
我的错误是:typeerror:test_function()缺少1所需的位置参数:'test_path'
谢谢您的宝贵时间!
im new to subprocessing and I have a question.
I can't find a proper solution online.
I want my path to be in a variable --> that will be passed to a function --> that will be passed to a subprocess.
I'm not allowed to show my real code, but this simple example (that I just can't get to work) would help me a lot.
This code snippet should do:
- Just take my path from a variable.
- "cd" the path in CMD.
- Open a file that is located in this path.
So far I tried:
import subprocess
test_path = "C:/randome_path/.."
def Test_Function(test_path):
subprocess.call("cd", test_path, shell = True)
subprocess.call("python file.py", shell = True)
Test_Function()
My ErrorMessage is:TypeError: Test_Function() missing 1 required positional argument: 'test_path'
Thank you for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要将参数传递给您的函数,因为这是您声明的方式:
或使用键值“方法”
第二:命令期望字符串而不是其他参数
注释1 执行此类命令
python file.py
假定python
的路径在某些环境变量中声明注2
subprocess
”下可能具有不同的“行为”在 shell = true 。现在应给出命令作为字符串列表
First you need to pass a parameter to your function because that's how you declarerd:
or using the key-value "approach"
Second: the command is expecting a string not a further parameter
Note 1 to execute such command
python file.py
it is assumed thatpython
's path is declared in some environment variableNote 2 that
subprocess
may have some different "behaviour" under WindowsTry without
shell=True
. Now the commands should be given as a list of strings