[Tutor] Hi all: How do I save a file in a designated folder?

Alan Gauld alan.gauld at yahoo.co.uk
Tue May 2 19:01:55 EDT 2017

On 02/05/17 19:09, Michael C wrote:
> from PIL import Image
> from PIL import ImageGrab
> screenshot = ImageGrab.grab()
> screenshot.show()
> screenshot.save("\test\missed.png")
> This is my current code, using Python Image Library!

You should probably investigate Pillow, I believe
development of PIL has now ceased in favour of Pillow.
Pillow is backwardly compatible with PIL so your
existing code should still work.

> What I would like to get help with is:
> 1. How to name the file with time stamp.   e.g.   05012017.png and so forth.

And so forth? I assume you mean

MMDDYYYY.png format?

You should read about the strftime function in the time
(and datetime) module. Other functions thee will find
the current date/time for you. Thee are examples on the
documentation pages but if you get stuck come back
with specific questioons.

> 2. How to save it in a designated folder like  C:\test\test2\ and so forth.

That is just string handling. thee is nothing special
about a file path(*), it is just a string. Store the folder as one
string then construct your file path with

filepath = folder + filename

(*)Having said that there is nothing special there is a
dedicated module for working with paths - os.path - that
has convenience functions for constructing and
de-constructing path strings. You might find it useful.

> P.S. I am on windows 10

The biggest issue with Windows is its use of \ as a separator.
Be sure to use raw strings ( ie prefix an r: r'....') or just
use a Unix style [path with / instead of \. Or use os.path
which will intelligently decide which separator to use.

Alan G
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:

More information about the Tutor mailing list