0

I was looking for an easy way to set a working folder at the beginning of my script to avoid having to change it each time I run my script from another machine. Sorry if it is silly but I can't find an answer without having to dive deeply into the sea of weird functions - and I don't know how to swim.

I would use it to save all images to the same specific folder.

My take:

#import stuff
filepath=raw_input('Provide the working directory: ')
#do stuff
plt.savefig(filepath+'\\image.jpg', dpi=2000,bbox='tight')

EDIT I have a number of files to save in one specific directory, the same where my script lives. But it is specified like this: C:\\Users\\User\\Desktop\\N_Scripts and it is on my own pc, but I oftentimes work on a different machine. How to preset the directory where all my images go to at the beginning of my script with something like raw_input?

Thank you for your help!

6
  • The error occurs before or during execution? Commented Oct 28, 2015 at 11:02
  • I didn't down-vote the question still my explanation for down vote would be I can't find a quick and easy answer not on your knowledge level. Commented Oct 28, 2015 at 11:03
  • @Don: it occurs right after having typed it. Commented Oct 28, 2015 at 11:04
  • 1
    @FrancescoCastellani I figure out you're using matplotlib. Can you provide all relevant parts of the script? (e.g. imports) Commented Oct 28, 2015 at 11:07
  • @J.F.Sebastian the idea is to get rid of having to retype a different directory each time I run my script from a different machine. How and where in the script would you use os.path.join? Before or after plt.savefig? Commented Oct 28, 2015 at 11:08

1 Answer 1

1

to save in one specific directory, the same where my script lives

To change the current working directory inside your Python script to a directory where your script lives:

import os

os.chdir(get_script_dir())

where get_script_dir() is defined here.

But then, once you use os.chdir, how do I modify the argument of plt.savefig to make sure my images are saved where my script is?
Would it just be plt.savefig(os.chdir(get_script_dir())+'\image.jpg)?

Use plt.savefig('image.jpg').

Sign up to request clarification or add additional context in comments.

3 Comments

So I guess it is not possible to use something like raw_input to allow the user to pre-determine the dir where he/she wants the files to be saved...
But then, once you use os.chdir, how do I modify the argument of plt.savefig to make sure my images are saved where my script is? Would it just be plt.savefig(os.chdir(get_script_dir())+'\\image.jpg)?
@FrancescoCastellani: you have asked "where my script lives". If you want something different; it is a different question. Obviously, you can ask user if you want (you could use raw_input() on Python 2, to ask from the command line or you could use tkFileDialog.askdirectory() to ask via GUI).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.