python windows路径原始字符串

2024-06-16 14:20:52 发布

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

当我跑的时候

path = "C:\windows\System32\wininit.exe"
sub = (subprocess.run(["powershell.exe", "Get-Filehash", path, "-Algorithm", "md5"], capture_output=True))

我得到这个错误:screenshot of output from "subprocess.run"

所以我试着:

path = r"C:\windows\System32\wininit.exe"
path = "C:/windows/System32/wininit.exe"
path = r"C:/windows/System32/wininit.exe"

仍然得到相同的错误,我已经没有选择了。 请帮助我解决这个问题,当我运行它时,它总是显示它正在尝试运行'c:\\windows\\system32\\winit.exe',即使它应该只有1个反斜杠

Update:我应该提到:代码适用于其他路径,但不适用于所有路径。对于那些不起作用的,我试着在powershell中单独运行 Get-Filehash path -Algorithm md5它可以工作,但当我用Python运行它时就不行了。因为就是这种情况,我认为代码可以工作,但对于某些文件它不能工作,尽管它应该工作。所以我想我需要一个解决方案


Tags: pathrun代码路径getwindows错误exe