16

I've found a few questions on the module but the more common problem seems to be getting the argument list right which I think I have managed (eventually)

I am trying to run a program that expects an input like this in the command line,

fits2ndf in out

with 'in' being the filepath of the file to be converted and 'out' being the path and filename to save the result to.

So using Subprocess,

subprocess.call(["fits2ndf","/media/tom_hdd/Transfer/reference.fits","/media/tom_hdd/Transfer/reference.sdf"])

this raises,

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Setting shell=TRUE (which I know is bad) produces the same result. Not sure if it is relevant but I am using tcsh. Any suggestions?


Edit in response to questions

I haven't permanently set the PATH however fits2ndf is part of a package of programs which I initialize using

% tcsh
% setenv STARLINK_DIR  /home/tomq/star-kapuahi
% source $STARLINK_DIR/etc/login
% source $STARLINK_DIR/etc/cshrc

and normally works from within any directory without specifying the full path.

user1889259
  • 245
  • 1
  • 3
  • 7
  • is `fits2ndf` in your path? – Karthik T Dec 09 '12 at 10:56
  • Sorry I don't quite understand your question, fits2ndf is contained in another directory but has run fine elsewhere without a full path, but it should not be part of the path to the .fits file. – user1889259 Dec 09 '12 at 10:59
  • 1
    @Karthik was referring to your PATH environment variable. Is the directory that contains `fits2ndf` in your $PATH, and is PATH exported? – cdarke Dec 09 '12 at 11:09
  • if you give full path of `fits2ndf` it should be OK. for ex: `/home/foo/fits2ndf` – denizeren Dec 09 '12 at 11:16
  • Ok time to admit that I am very new to unix and get by by following tutorials and asking questions. fits2ndf is almost certainly not in my path. QUESTION EDITED as I cant format code down here properly? – user1889259 Dec 09 '12 at 11:19

2 Answers2

14

which fits2ndf will show you the path of fits2ndf.

After that you can write given full path to your code and it should work.

Ex:

~$ which mv
/bin/mv

My python code:

import subprocess

subprocess.call(["/bin/mv","/tmp/a","/tmp/b"])
denizeren
  • 934
  • 8
  • 20
  • 3
    Thanks this worked, is there away of getting it so I dont need to specify the full path to fits2ndf each time? In fact ideally so that I can use any of the programs within that folder without the path? It makes things pretty messy. – user1889259 Dec 09 '12 at 11:35
  • You can get the system `PATH` from `os.environ['PATH']` and modify it, then pass it in to `subprocess` in the `env` keyword parameter. Notice that `env` wants a dict; you might copy all of `os.environ` and modify the `PATH` in the copy, though commonly the subprocess you run doesn't require anything else to be set in the environment, and so it's sufficient to create a new dict with just the `PATH` member. – tripleee Dec 20 '20 at 11:55
0

You might want to remove the space in " /media/tom_hdd/Transfer/reference.sdf"

Also, try tp put everything in one string, like "fits2ndf /media/tom_hdd/Transfer/reference.fits /media/tom_hdd/Transfer/reference.sdf"

Make sure you point to the exact direction.

Lotzki
  • 489
  • 1
  • 4
  • 18
  • good spot, thanks. Unfortunately that only appeared when writing this question and not in the code so it hasn't changed anything. – user1889259 Dec 09 '12 at 11:03