5

I have a python script, which generates files. All i want is to force it to write the files in a specific folder. Right now i have to do 3 steps:

cd foo
python ../awesome_script.py
cd ..

Is there any nice solution, where i can do this in one line, either using some external command, or directly in the python interpreter?

I am searching for something like:

python -f foo awesome_script.py

or

cd_in_and_out_program foo awesome_script.py

This instruction will be in a makefile afterwards, so it can be ugly.

JClarke
  • 788
  • 1
  • 9
  • 22
hr0m
  • 2,643
  • 5
  • 28
  • 39
  • 3
    `(cd foo && python awesome_script.py)` (with the parenthesises) – spectras Aug 02 '16 at 15:24
  • if you wrote to stdout ie print() you could do somethong like python foo.py > /path/to/file.mk – Matt Joyce Aug 02 '16 at 15:24
  • @MattJoyce no, i am creating files – hr0m Aug 02 '16 at 15:25
  • You could do as @MattJoyce suggests (my preferred solution since it allows the script to be more flexible) or you could take the output folder as a command line argument which defaults to the current directory. – Rick Aug 02 '16 at 15:25
  • @spectras has probably the best version, and he is absolutely right with the duplicate :) – hr0m Aug 02 '16 at 15:28

1 Answers1

2

If the problem is just "one line":

cd foo; python ../awesome_script.py; cd ..

will do

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31