簡體   English   中英

在Ubuntu上使用終端運行文件:os.system在subprocess.Popen找不到文件的地方起作用

[英]Running a file using terminal on Ubuntu: os.system is functional where subprocess.Popen cannot find the file

我試圖通過使用python運行文件subprocess.Popen在Ubuntu版本16.一種開源應用的安裝和使用命令CopasiUI在終端將打開Copasi GUI而命令CopasiSE在終端打開的命令行界面到同一程序。 使用CopasiSE <file path>其中<file path>是copasi文件的完整路徑)將提交copasi文件以供運行。 手動完成后,這將完全起作用。

代碼

In [13]: f='/home/b3053674/Documents/PyCoTools/PyCoTools/PyCoToolsTutorial/Kholodenko_0.cps'

In [14]: import os

In [15]: f
Out[15]: '/home/b3053674/Documents/PyCoTools/PyCoTools/PyCoToolsTutorial/Kholodenko_0.cps'

In [16]: os.path.isfile(f)
Out[16]: True

該程序的符號鏈接也起作用:

In [20]: subprocess.Popen('CopasiSE')
Out[20]: <subprocess.Popen at 0x7f72a2643e90>

In [21]: COPASI 4.16 (Build 104)
The use of this software indicates the acceptance of the attached license.
To view the license please use the option: --license

Usage: CopasiSE [options] [file]
  --SBMLSchema schema           The Schema of the SBML file to export.
  --configdir dir               The configuration directory for copasi. The
                                default is .copasi in the home directory.
  --configfile file             The configuration file for copasi. The
                                default is copasi in the ConfigDir.
  --exportBerkeleyMadonna file  The Berkeley Madonna file to export.
  --exportC file                The C code file to export.
  --exportXPPAUT file           The XPPAUT file to export.
  --home dir                    Your home directory.
  --license                     Display the license.
  --maxTime seconds             The maximal time CopasiSE may run in
                                seconds.
  --nologo                      Surpresses the startup message.
  --validate                    Only validate the given input file (COPASI,
                                Gepasi, or SBML) without performing any
                                calculations.
  --verbose                     Enable output of messages during runtime to
                                std::error.
  -c, --copasidir dir           The COPASI installation directory.
  -e, --exportSBML file         The SBML file to export.
  -i, --importSBML file         A SBML file to import.
  -s, --save file               The file the model is saved to after work.
  -t, --tmp dir                 The temp directory used for autosave.

使用os.system可以:

In [21]: os.system('CopasiSE {}'.format(f))
COPASI 4.16 (Build 104)
The use of this software indicates the acceptance of the attached license.
To view the license please use the option: --license

這是正在運行的Copasi文件的預期輸出。

但是subprocess.Popen給我這個:

In [22]: subprocess.Popen('CopasiSE {}'.format(f))
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-22-c8cd60af5d46> in <module>()
----> 1 subprocess.Popen('CopasiSE {}'.format(f))

/home/b3053674/anaconda2/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)
    388                                 p2cread, p2cwrite,
    389                                 c2pread, c2pwrite,
--> 390                                 errread, errwrite)
    391         except Exception:
    392             # Preserve original exception in case os.close raises.

/home/b3053674/anaconda2/lib/python2.7/subprocess.pyc in _execute_child(self, args, executable, preexec_fn, close_fds, cwd, env, universal_newlines, startupinfo, creationflags, shell, to_close, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite)
   1022                         raise
   1023                 child_exception = pickle.loads(data)
-> 1024                 raise child_exception
   1025 
   1026 

OSError: [Errno 2] No such file or directory

In [23]: 

有人可以建議原因嗎?

使用subprocess.Popen(['CopasiSE', f])應該分別傳遞參數: subprocess.Popen(['CopasiSE', f])因為它是分割命令行的外殼程序(由os.system()調用)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM