Python os.system存在时找不到路径

2024-10-02 04:24:18 发布

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

我想用python启动WindowsSandbox.exe,它位于C:\Windows\System32\下。但是出于某种原因,它就是不起作用,尽管另一个可执行文件(cmd.exe)位于完全相同的位置

不知道为什么会发生这种情况(环境:Windows 10 1909 Python 3.8.0):

命令:

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运行)

任何帮助都将不胜感激,谢谢


Tags: ortocmdoswindowsbuffercalcable
1条回答
网友
1楼 · 发布于 2024-10-02 04:24:18

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

相关问题 更多 >

    热门问题