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!

jfs
  • 399,953
  • 195
  • 994
  • 1,670
FaCoffee
  • 7,609
  • 28
  • 99
  • 174
  • The error occurs before or during execution? – Don Oct 28 '15 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. – WoodChopper Oct 28 '15 at 11:03
  • @Don: it occurs right after having typed it. – FaCoffee Oct 28 '15 at 11:04
  • 1
    @FrancescoCastellani I figure out you're using matplotlib. Can you provide all relevant parts of the script? (e.g. imports) – Don Oct 28 '15 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`? – FaCoffee Oct 28 '15 at 11:08
  • @Don These are my imports: `import numpy`, `from numpy import *`, `import networkx as nx`, `from networkx import *`, `import matplotlib.pyplot as plt`, `import csv`, `from collections import *`, `from __future__ import division`, `import os`. – FaCoffee Oct 28 '15 at 11:09

1 Answers1

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').

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 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... – FaCoffee Oct 28 '15 at 11:29
  • 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)`? – FaCoffee Oct 28 '15 at 11:31
  • @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). – jfs Oct 28 '15 at 11:32