0

Basically, I'm trying to navigate through directories and call a specific program (called 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. However, there are dozens of files, and individually running each file through galfit would take far too long. 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:

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; however, there are about fifty files in total (with different names, of course!) that I wish to process.

So my question is, how do you approach that through 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?

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
vdogsandman
  • 5,289
  • 8
  • 21
  • 21

1 Answers1

0

Take a look at os.path for directory navigation. To execute a shell command use 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)
mjszczep
  • 48
  • 2
  • Interesting, thank you! Out of curiosity, what does "%s % file" do? – vdogsandman Jul 09 '13 at 21:26
  • also, os.system() does execute other programs, but sooner or later subprocess.popen() might be of interest: http://stackoverflow.com/questions/12605498/how-to-use-subprocess-popen-python – mnagel Jul 09 '13 at 21:34
  • It's C-style string formatting, as detailed [here](http://docs.python.org/2/library/stdtypes.html#string-formatting). Technically though, it's deprecated since Python 3.1 and the recommended syntax is now [.format](http://docs.python.org/2/library/stdtypes.html#str.format). – mjszczep Jul 09 '13 at 21:37
  • This is very helpful, thank you! Is there a method for simply running a command as is, like "~/galfit f105w0.feedme" instead of having to open a program or something? Sorry for the repeated questions, it's just that this is all very new and very fascinating to me. – vdogsandman Jul 09 '13 at 21:40
  • If you're looking for a way to do something like this more natively, try shell scripts. The [Advanced BASH Scripting Guide](http://www.tldp.org/LDP/abs/html/) could get you started. – mjszczep Jul 09 '13 at 21:57