Re: [Tutor] Hi all: How do I save a file in a designated folder?
On 03/05/17 00:28, Cameron Simpson wrote: >> And so forth? I assume you mean >> >> MMDD.png format? >> >> You should read about the strftime function in the time > > Further to this, I would also advocate that you consider writing the > timestamp > from largest unit to smallest unit, like an ISO8601 timestamp, which > typically > looks like: > > MMDD.png Yes, I absolutely second this advice and should have included it. ISO dates make programming life so much easier! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi all: How do I save a file in a designated folder?
On Tue, May 2, 2017 at 6:09 PM, Michael Cwrote: > screenshot.save("\test\missed.png") You probably know that "\t" represents a tab in a string literal, but there's something about working with a path that causes people to overlook this. Windows won't overlook it. Control characters, i.e. characters with an ordinal value below 32, are forbidden in Windows file names. Also, you're depending on whatever drive or UNC path is the current directory. For example: >>> os.chdir('C:\\') >>> os.path.abspath('/test/missed.png') 'C:\\test\\missed.png' >>> os.chdir('localhost\C$') >>> os.path.abspath('/test/missed.png') 'localhost\\C$\\test\\missed.png' A fully-qualified path must start with the drive or UNC share. For example: >>> os.path.abspath('C:/test/missed.png') 'C:\\test\\missed.png' >>> os.path.abspath('//localhost/C$/test/missed.png') 'localhost\\C$\\test\\missed.png' In this case I'm using abspath() to normalize the path, which first has Windows itself normalize the path via GetFullPathName(). This shows what Windows will actually try to open or create, given that it implements legacy DOS rules. For example, it ignores trailing spaces and dots in the final path component: >>> os.path.abspath('C:/test/missed.png ... ') 'C:\\test\\missed.png' and DOS devices are virtually present in every directory: >>> os.path.abspath('C:/test/nul.txt') '.\\nul' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi all: How do I save a file in a designated folder?
On 03May2017 00:01, Alan Gauldwrote: On 02/05/17 19:09, Michael C wrote: 1. How to name the file with time stamp. e.g. 05012017.png and so forth. And so forth? I assume you mean MMDD.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. Further to this, I would also advocate that you consider writing the timestamp from largest unit to smallest unit, like an ISO8601 timestamp, which typically looks like: MMDD.png See: https://www.iso.org/iso-8601-date-and-time-format.html https://en.wikipedia.org/wiki/ISO_8601 This has the advantage that a lexical sort (as in a typical directory listing or file glob) automatically arranges names in date order. I'd also remark that the USA centric format (MMDD) that Alan inferred is particularly unfortunate in that (a) the rest of the world generally use DDMM so that the units come in increasing size and (b) doesn't sort very nicely at all. Of course, you might have meant 5th December 2017 above, so who knows? But the very ambiguity makes this one to avoid. For numeric timestamps like yours I only ever use MMDD (or the obvious variants like -MM-DD etc). If I use another, I make the month a word (lousy for sorting and parsing, but clear to the human reader); I only do that in prose, not in filenames. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi all: How do I save a file in a designated folder?
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 MMDD.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. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Hi all: How do I save a file in a designated folder?
from PIL import Image from PIL import ImageGrab # takes the screenshot screenshot = ImageGrab.grab() # display the screenshot screenshot.show() # save the screenshot screenshot.save("\test\missed.png") This is my current code, using Python Image Library! 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. 2. How to save it in a designated folder like C:\test\test2\ and so forth. P.S. I am on windows 10 Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor