Python子进程调用

时间:2014-08-24 21:51:59

标签: python subprocess

我在通过python运行文件时遇到问题。 这是我的代码:

def report1(self):
   str="/Users/Apple/Desktop/Report1.exe"
   subprocess.call(str)


This is the error i am getting : 
File "./DBMS.py", line 427, in <module>
Main().run();
File "./DBMS.py", line 415, in run
self.report1()
File "./DBMS.py", line 383, in report1
subprocess.call(str)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py",      line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1308, in _execute_child
raise child_exception

OSError:[Errno 13]权限被拒绝

PS:我尝试更改文件夹的权限,我尝试使用subprocress.Popen。 我也尝试添加shell = True。我不明白为什么它仍无法正常工作。

任何帮助都非常感谢。 必须在24小时内提交:(

2 个答案:

答案 0 :(得分:1)

尽管尝试执行命令时发生错误,但subprocess并未充分说明其优点。

如果追溯中最深的帧(实际异常之前的那一帧)来自raise child_exception subprocess.py,则表示fork(2)和{{1}之间存在一些问题调用 - 换句话说,在尝试运行您请求的命令时发生错误。

您粘贴的实际例外是exec*(2)OSError: [Errno 13] Permission denied的{​​{1}}对应errno

13

如果您从未使用EACCES>>> import errno; print errno.errorcode[13] EACCES ,那么事情就会非常难以理解,因为fork(2)已经放弃了真正的追溯。但是,我可以告诉你,这exec(2)几乎肯定来自subprocess电话。事实证明OSError在以下条件下提出了这个问题:

exec*

(由Apple提供)

如果我不得不猜测,您遇到了这个异常,因为您尝试运行的命令没有标记为可执行文件(类似execve)。

现在,解决此问题后,您的 [EACCES] Search permission is denied for a component of the path prefix. [EACCES] The new process file is not an ordinary file. [EACCES] The new process file mode denies execute permission. [EACCES] The new process file is on a filesystem mounted with execution disabled (MNT_NOEXEC in <sys/mount.h>). 文件不太可能在您的Mac上运行,但至少它会是一个不同的错误!

答案 1 :(得分:0)

尝试将程序作为sudo运行:

sudo python mycode.py