Python OS系统无法找到路径

时间:2020-02-02 15:11:33

标签: python python-3.x cmd path

我想使用WindowsSandbox.exe下的python启动C:\Windows\System32\。但是由于某种原因,尽管另一个可执行文件(cmd.exe)处于完全相同的位置,但是它根本无法工作。

不知道为什么会这样(环境:Windows 10 1909 Python 3.8.0):

CMD:

C:\>where calc.exe
C:\Windows\System32\calc.exe

C:\>where WindowsSandbox.exe
C:\Windows\System32\WindowsSandbox.exe

Python:

Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:21:23) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.system("calc.exe") # Opens calculator without any problem
0
>>> os.system("C:/Windows/System32/calc.exe") # Opens calculator without any problem
0
>>> os.system("WindowsSandbox.exe")
'WindowsSandbox.exe' is not recognized as an internal or external command,
operable program or batch file.
1
>>> os.system("C:/Windows/System32/WindowsSandbox.exe")
'C:/Windows/System32/WindowsSandbox.exe' is not recognized as an internal or external command,
operable program or batch file.
1
>>>

Nodejs(仅用于证明它应在python上工作):

Welcome to Node.js v12.13.1.
Type ".help" for more information.
> const { execSync } = require("child_process")
undefined
> execSync('calc.exe') //Able to launch calculator
<Buffer >
> execSync('C:/Windows/System32/calc.exe') //Able to launch calculator
<Buffer >
> execSync('WindowsSandbox.exe') //Able to launch Windows Sandbox
<Buffer >
> execSync('C:/Windows/System32/WindowsSandbox.exe') //Able to launch Windows Sandbox
<Buffer >
> 

我还尝试了双转义('C:\\Windows\\System32\\WindowsSandbox.exe'),原始字符串(r'C:\Windows\System32\WindowsSandbox.exe')和反斜杠('C:/Windows/System32/WindowsSandbox.exe')。它们都不起作用

%PATH%应该没有问题(因为cmd.exeWindowsSandbox.exe都可以直接从cmd运行)。

谢谢您的任何帮助。

1 个答案:

答案 0 :(得分:0)

os.path模块具有修复目录路径信息,验证目标是否存在等方法。