简体   繁体   English

Ubuntu / Python-如何通过Python调用ubuntu命令和第三方应用程序

[英]Ubuntu/Python- How to call ubuntu commands and 3rd party applications through Python

Basically, I'm trying to navigate through directories and call a specific program (called galfit ). 基本上,我试图浏览目录并调用特定程序(称为galfit )。 The reason I navigate through the directories is because all of the files that I want to run through galfit are in that directory. 我浏览目录的原因是因为我要通过galfit运行的所有文件都在该目录中。 However, there are dozens of files, and individually running each file through galfit would take far too long. 但是,有数十个文件,并且通过galfit单独运行每个文件将花费太长时间。 On top of that, they take a while to process, so the overall process is incredibly slow. 最重要的是,它们需要一段时间才能处理,因此整个过程非常缓慢。

Here's what the Ubuntu terminal code looks like: 这是Ubuntu终端代码的样子:

vidur@vidur-VirtualBox:~$ cd Documents
vidur@vidur-VirtualBox:~/Documents$ cd XDF_Thumbnails_sci
vidur@vidur-VirtualBox:~/Documents$ ls
documents-export-2013-07-08  XDF_Images_Sci  XDF_Images_Wht  XDF_Thumbnails_Sci
vidur@vidur-VirtualBox:~/Documents$ cd XDF_Thumbnails_Sci
vidur@vidur-VirtualBox:~/Documents/XDF_Thumbnails_Sci$ ~/galfit galfit.feedme

galfit.feedme is the feedme file that I wish to process; galfit.feedme是我要处理的feedme文件; however, there are about fifty files in total (with different names, of course!) that I wish to process. 但是,我总共要处理大约50个文件(当然,它们的名称不同!)。

So my question is, how do you approach that through Python? 所以我的问题是,您如何通过Python处理该问题? Eventually I'll be looping through all the files (and likely somehow auto-naming them, that's easy), but what's the process to get to the directory and then run galfit ? 最终,我将遍历所有文件(并且很可能以某种方式自动命名它们,这很容易),但是进入目录然后运行galfit什么?

Take a look at os.path for directory navigation. 查看用于目录导航的os.path To execute a shell command use os.system . 要执行shell命令,请使用os.system The example you posted could go something along the lines of: 您发布的示例可能类似于以下内容:

os.chdir(os.path.expanduser('~/Documents/XDF_Thumbnails_Sci'))
for file in os.listdir('.'):
    if os.path.splitext(file)[1] == ".feedme":
        os.system("~/galfit %s" % file)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM