This is a test version of Biostars. For the public version, visit https://www.biostars.org.
command line python script run on a file in different directory

I have a script.py in /Users/admin/Desktop and I want to run this script on a file that is in /Users/admin//Desktop/folder/file.txt, without changing the present path (i.e. /Users/admin/Desktop)

Question: what is the most efficient way to do that on command-line ? I am using the following commands and results are not as expected.

$ python script.py --script; /Users/admin/Desktop/file.txt

     raise StopIteration('because of missing file (file.txt)')

StopIteration: because of missing file (file.txt)

so far I have tried the following commands:

Fail #1:

python script.py  && /Users/admin/Desktop/folder/file.txt
StopIteration: because of missing file (file.txt)

Fail #2

python script.py;  /Users/admin/Desktop/folder/file.txt
StopIteration: because of missing file (file.txt)

Fail #3

python script.py -C ~/Users/admin/Desktop/folder/file.txt
StopIteration: because of missing file (file.txt)
python

Did you try python script.py /Users/admin/Desktop/file.txt? Should work if the script is written in such a way that it can parse the file path you are providing. If the script expects the file to be in the local directory then you could make a soft link to the file locally and see if that works. I assume you are asking this because you are not able to modify the script yourself.

Your attempts #1 and #2 certainly failed because you are running first script.py without arguments and then you try to run /Users/admin/Desktop/folder/file.txt on its own, which probably makes no sense. This is because you have ; or && separating script.py and the input.

It would help us if you would show us the content of the script. Most likely what genomax2 and Devon Ryan described will work. It's rather bad practice to name your script 'script.py', so I hope that's not the real name. If that script always has to work on the same file you could hard-code it in the script so you don't have to specify it on the command line.

This is not really a bioinformatics question so this might get closed.

This is clearly not a problem of different directories between your file and the script, but mostly because you don't understand how scripts (and probably unix system) works.

I suggest you to first read some unix tutorial and come here again with real bioinformatics question.

Hello ahmedakhokhar!

We believe that this post does not fit the main topic of this site.

This is not a bioinformatics question. Please google/search stack overflow.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

1 answer

The most efficient way is either:

python script.py folder/file.txt

or

./script.py folder/file.txt

BTW, this is incredibly off-topic.

Log in to answer this question.