2012-08-10 63 views
1

當我嘗試執行與subprocess.Popen命令。我得到這個錯誤;無法執行與子進程shell命令.Popen

In [3]: subprocess.Popen(["su - dhclient","eth0"]) 
--------------------------------------------------------------------------- 
OSError         Traceback (most recent call last) 
/home/oae/<ipython-input-3-1cd768a39571> in <module>() 
----> 1 subprocess.Popen(["su - dhclient","eth0"]) 

/usr/lib/python2.7/subprocess.pyc in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags) 
    677        p2cread, p2cwrite, 
    678        c2pread, c2pwrite, 
--> 679        errread, errwrite) 
    680 
    681   if mswindows: 

/usr/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite) 
    1247      if fd is not None: 
    1248       os.close(fd) 
-> 1249     raise child_exception 
    1250 
    1251 

OSError: [Errno 2] No such file or directory 

我該如何解決這個問題?謝謝。

回答

2

您可以使用:

subprocess.Popen("su - dhclient eth0", shell=True) 
+0

感謝。這是行得通的。我需要添加shell = True。 – 2012-08-10 12:53:24

+0

'shell = True'通常被視爲一個安全問題,所以不要過度使用它。但是,在這種情況下,這是正確的做法。 – 2012-08-10 13:13:31