2

I would like to save a python plot. With MacOS everything runs smoothly but it fails under Windows7.

The python-command looks like this:

savefig('C:\Users\xyz\AppData\Local\Temp\pyplot1468046843481608342.png')

The error messge is this one:

Traceback (most recent call last):
File "C:\Users\xyz\AppData\Local\Temp\analyze3744796441786382480.py", line 340, in <module>
savefig('C:\Users\xyz\AppData\Local\Temp\pyplot1468046843481608342.png')
File "C:\Anaconda\lib\site-packages\matplotlib\pyplot.py", line 577, in savefig
res = fig.savefig(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\figure.py", line 1470, in savefig
self.canvas.print_figure(*args, **kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backend_bases.py", line 2194, in print_figure
**kwargs)
File "C:\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py", line 526, in print_png
filename_or_obj = open(filename_or_obj, 'wb')
IOError: [Errno 22] invalid mode ('wb') or filename: 'C:\\Users\xyz\\AppData\\Local\\Temp\\pyplot1468046843481608342.png'

This is the Python I'm using:

Python 2.7.8 :: Anaconda 2.1.0 (32-bit)

Can anybody explain the cause of the problem?

Antje Janosch
  • 1,154
  • 5
  • 19
  • 37
  • IIRC, you can use forward slashes also on windows `savefig('C:/shorter/path/pyplot1468046843481608342.png')` and that should work as is. OTOH the clean and portable way of constructing pathnames is to use the [`os.path`](http://pymotw.com/2/ospath/) module. A quote from the link above: "Writing code to work with files on multiple platforms is easy using the functions included in the os.path module. Even programs not intended to be ported between platforms should use os.path for reliable filename parsing." – gboffi Dec 08 '14 at 10:22
  • thanks for the hint working with os.path. I'll give it a look. the pathnames were generated with java using the Java.io.File.createTempFile(). That's why, I would not change anything by hand. – Antje Janosch Dec 08 '14 at 13:18

1 Answers1

3

I think you have to add r before your directory to convert your string into raw string. See this post

Unknown python expression filename=r'/path/to/file'

Community
  • 1
  • 1
GioR
  • 596
  • 2
  • 8
  • 19