使用子进程从另一个驱动器调用程序时获取权限错误
Getting permission error when calling a program from another drive with subprocess
我有一个包含 4 行代码的脚本:
import subprocess
folderAdd = r"D:\Program Files (x86)\someapp\"
result = subprocess.run(['"' + folderAdd +'someProgram.exe"','"' + folderAdd +'somefile.ext"'], stdout=subprocess.PIPE)
print(result.stdout)
我稍微编辑了代码以删除 files/folders 的细节(因为我认为这不是问题所在?)。 someProgram.exe 是我制作的 go lang 程序,somefile.ext 是我使用命令行传递给 go land 程序的文件(例如命令行中的语法是:“someProgram.ext somefile.ext”。我遇到的问题是当我 运行 这个脚本(存储在我的 E 驱动器上 - 所以这是工作目录)时我得到以下错误:
PermissionError: [WinError 5] Access is denied
我已经尝试从 spyder(我选择的 ide)和命令行 运行ning 这个 python 脚本。这两个我都试过 运行ning 作为管理员 (based on this question/answer)。我仍然得到相同的权限错误。还有其他解决方法吗?
您要添加不应该在此处的双引号。
您还应该使用 python 的工具加入路径:os.path.join
(https://docs.python.org/3.8/library/os.path.html#os.path.join)
如果你只有什么stdout,你可以使用subprocess.check_outout
(https://docs.python.org/3.8/library/subprocess.html#subprocess.check_output)
import subprocess
folderAdd = r"D:\Program Files (x86)\someapp"
print(subprocess.check_output([os.path.join(folderAdd, 'someProgram.exe'), os.path.join(folderAdd, 'somefile.ext'])
我有一个包含 4 行代码的脚本:
import subprocess
folderAdd = r"D:\Program Files (x86)\someapp\"
result = subprocess.run(['"' + folderAdd +'someProgram.exe"','"' + folderAdd +'somefile.ext"'], stdout=subprocess.PIPE)
print(result.stdout)
我稍微编辑了代码以删除 files/folders 的细节(因为我认为这不是问题所在?)。 someProgram.exe 是我制作的 go lang 程序,somefile.ext 是我使用命令行传递给 go land 程序的文件(例如命令行中的语法是:“someProgram.ext somefile.ext”。我遇到的问题是当我 运行 这个脚本(存储在我的 E 驱动器上 - 所以这是工作目录)时我得到以下错误:
PermissionError: [WinError 5] Access is denied
我已经尝试从 spyder(我选择的 ide)和命令行 运行ning 这个 python 脚本。这两个我都试过 运行ning 作为管理员 (based on this question/answer)。我仍然得到相同的权限错误。还有其他解决方法吗?
您要添加不应该在此处的双引号。
您还应该使用 python 的工具加入路径:os.path.join
(https://docs.python.org/3.8/library/os.path.html#os.path.join)
如果你只有什么stdout,你可以使用subprocess.check_outout
(https://docs.python.org/3.8/library/subprocess.html#subprocess.check_output)
import subprocess
folderAdd = r"D:\Program Files (x86)\someapp"
print(subprocess.check_output([os.path.join(folderAdd, 'someProgram.exe'), os.path.join(folderAdd, 'somefile.ext'])