python中的getstatusoutput返回错误的resu

2024-10-01 15:39:13 发布

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

我正在写一个方法,在Mac浏览器窗口中打开一个路径。所以,我在python中使用commands模块的getstatusoutput方法。我有以下代码:

Path = "file:///" + USERHOME
filePath = os.path.join(Path, filePath)

ret, out = commands.getstatusoutput('open "%s"' % filePath)

此方法适用于大多数文件/文件夹,但名为“old,;'{}&;的文件/文件夹除外。在本例中,如果我没有像上面提到的那样将“file:///”放在实际路径之前,它就可以工作了。我不明白为什么会这样。有人能指出我在这里遗漏了什么吗?你知道吗


Tags: 模块文件path方法代码路径文件夹os
1条回答
网友
1楼 · 发布于 2024-10-01 15:39:13

如果您使用的是2.7+版本,请不要使用“命令”模块。它被弃用并被subprocess module替换。你知道吗

其次,不要使用file:///前缀。使用-R标志告诉open在Finder中显示它。从打开的手册页:

-R Reveals the file(s) in the Finder instead of opening them.

示例:

import subprocess
subprocess.Popen(["open", "-R", "/path/to/myfile/old,;'{}&"])

相关问题 更多 >

    热门问题