Saving to a file in a specified directory (and creating said directory along the way)

Hey,

I'm working on a project for which I would like to dump data into a
file in a specified directory (that doesn't necessarily exist yet). I
know matplotlib.mlab.save(fname, X) will only work if I want to save
data from an array/list to a file in the current working directory. Is
there an easy way using matplotlib to:

A) Save a file to a specified directory rather than the current
working directory
B) Create a directory I tell it to save to if it doesn;t already exist

Thanks in advance.

Josh

Hey,

I'm working on a project for which I would like to dump data into a
file in a specified directory (that doesn't necessarily exist yet). I
know matplotlib.mlab.save(fname, X) will only work if I want to save
data from an array/list to a file in the current working directory. Is
there an easy way using matplotlib to:

A) Save a file to a specified directory rather than the current
working directory

Just specify the full path to the output dir:

  save('/path/to/my/data.dat')

or use the os modules built-in functions for cross-platform path handling:

  save(os.path.join(outdir, filename))

B) Create a directory I tell it to save to if it doesn;t already exist

You can use os.mkdir. mpl svn has a function cbook.mkdirs to
recursively make a directory path.

JDH

···

On Thu, Nov 20, 2008 at 10:24 AM, Joshua Lippai <discerptor@...287...> wrote:

Ah, silly me. I forgot to notice I was using a path variable in the
string I was giving to mlab.save. Part A works just fine when I
account for that. Now that just leaves creating the directory. I like
the cbook.mkdirs implementation a lot better than the non-recursive
version in os... thanks for the tip!

Josh

···

On Thu, Nov 20, 2008 at 8:48 AM, John Hunter <jdh2358@...287...> wrote:

On Thu, Nov 20, 2008 at 10:24 AM, Joshua Lippai <discerptor@...287...> wrote:

Hey,

I'm working on a project for which I would like to dump data into a
file in a specified directory (that doesn't necessarily exist yet). I
know matplotlib.mlab.save(fname, X) will only work if I want to save
data from an array/list to a file in the current working directory. Is
there an easy way using matplotlib to:

A) Save a file to a specified directory rather than the current
working directory

Just specify the full path to the output dir:

save('/path/to/my/data.dat')

or use the os modules built-in functions for cross-platform path handling:

save(os.path.join(outdir, filename))

B) Create a directory I tell it to save to if it doesn;t already exist

You can use os.mkdir. mpl svn has a function cbook.mkdirs to
recursively make a directory path.

JDH

Joshua Lippai wrote:

Now that just leaves creating the directory. I like
the cbook.mkdirs implementation a lot better than the non-recursive
version in os... thanks for the tip!

why not os.makedirs() ?

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

Wow, that one went right by me -- somehow missed its existence.
Thanks for the pointer...

JDH

···

On Thu, Nov 20, 2008 at 11:22 AM, Christopher Barker <Chris.Barker@...259...> wrote:

Joshua Lippai wrote:

Now that just leaves creating the directory. I like
the cbook.mkdirs implementation a lot better than the non-recursive
version in os... thanks for the tip!

why not os.makedirs() ?

cbook.mkdirs includes a few if-then calls that only make the directory
if it doesn't already exist (in fact it actually calls os.makedirs in
the function), whereas os.makedirs raises an OSError if the folder I
specified already exists. For my purposes, the cbook implementation is
more convenient.

Josh

···

On Thu, Nov 20, 2008 at 9:22 AM, Christopher Barker <Chris.Barker@...259...> wrote:

Joshua Lippai wrote:

Now that just leaves creating the directory. I like
the cbook.mkdirs implementation a lot better than the non-recursive
version in os... thanks for the tip!

why not os.makedirs() ?

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

But given the vagueness of what I asked, you're right, os.makedirs was
a good suggestion too. Thanks. I think I need to sit down and spend
some quality time with the os module over Thanksgiving holiday...

Josh

···

On Thu, Nov 20, 2008 at 9:30 AM, Joshua Lippai <discerptor@...287...> wrote:

cbook.mkdirs includes a few if-then calls that only make the directory
if it doesn't already exist (in fact it actually calls os.makedirs in
the function), whereas os.makedirs raises an OSError if the folder I
specified already exists. For my purposes, the cbook implementation is
more convenient.

Josh

On Thu, Nov 20, 2008 at 9:22 AM, Christopher Barker > <Chris.Barker@...259...> wrote:

Joshua Lippai wrote:

Now that just leaves creating the directory. I like
the cbook.mkdirs implementation a lot better than the non-recursive
version in os... thanks for the tip!

why not os.makedirs() ?

-Chris

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@...259...

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users