使用subprocess call/popen从另一个python脚本执行python脚本

2024-04-24 06:04:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我在从不同的python脚本执行python脚本时遇到问题:

filePath = "C:\Desktop\TestScripts\test.py"
####subprocess.call(filePath, shell = True)

child = subprocess.Popen(filePath, shell=True, stderr=subprocess.PIPE)
   while True:
        out = child.stderr.read(1)
        if out == '' and child.poll() != None:
           break
        if out != '':
           sys.stdout.write(out)
           sys.stdout.flush()

当我执行此操作时,我得到:

'C:\Desktop\TestScripts\' is not recognized as an internal or external command,
operable program or batch file.

我无法解释上述错误。如有任何建议,我们将不胜感激。你知道吗


Tags: ortest脚本childtrueifstderrstdout
1条回答
网友
1楼 · 发布于 2024-04-24 06:04:57

注意,错误是说找不到文件'C:\Desktop\TestScripts\'。文件名被截断,因为'\t'是制表符。你知道吗

In [156]: print('C:\Desktop\TestScripts\test.py')
C:\Desktop\TestScripts  est.py

改用raw stringr'C:\Desktop\TestScripts\test.py'。你知道吗

In [157]: print(r'C:\Desktop\TestScripts\test.py')
C:\Desktop\TestScripts\test.py

相关问题 更多 >