将路径变量传递给Python的子过程

发布于 2025-02-07 17:50:41 字数 781 浏览 0 评论 0原文

我是子处理的新手,我有一个问题。 我在网上找不到适当的解决方案。

我希望我的道路进入变量 - >那将传递给功能 - >那将传递给子过程。
我不允许显示我的真实代码,但是这个简单的例子(我无法上班)会对我有很大帮助。
此代码段应该要做:

  1. 只需从变量中走我的路即可。
  2. “ CD” CMD中的路径。
  3. 打开位于此路径中的文件。

到目前为止我尝试过:

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:

  1. Just take my path from a variable.
  2. "cd" the path in CMD.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花落人离去 2025-02-14 17:50:42

首先,您需要将参数传递给您的函数,因为这是您声明的方式:

Test_Function(test_path) # here the function call with parameter 

或使用键值“方法”

another_path = # ...
Test_Function(test_path=another_path)

第二:命令期望字符串而不是其他参数

subprocess.call(f"python file.py", shell=True, cwd=test_path) 

注释1 执行此类命令python file.py假定python的路径在某些环境变量中声明

注2 subprocess ”下可能具有不同的“行为”

shell = true 。现在应给出命令作为字符串列表

def Test_Function(test_path):         
    subprocess.call(["python", "file.py"], cwd=test_path)

First you need to pass a parameter to your function because that's how you declarerd:

Test_Function(test_path) # here the function call with parameter 

or using the key-value "approach"

another_path = # ...
Test_Function(test_path=another_path)

Second: the command is expecting a string not a further parameter

subprocess.call(f"python file.py", shell=True, cwd=test_path) 

Note 1 to execute such command python file.py it is assumed that python's path is declared in some environment variable

Note 2 that subprocess may have some different "behaviour" under Windows

Try without shell=True. Now the commands should be given as a list of strings

def Test_Function(test_path):         
    subprocess.call(["python", "file.py"], cwd=test_path)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文