Python Forum

Full Version: datetime with every screenshot name
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello all ...
i write a code to take 10 screenshot from the desktop and save them to file but i need to save them in this format :

qan-2018-08-27 04:03(0)
qan-2018-08-27 04:03(1)
qan-2018-08-27 04:03(2)
...


this is my code :
import pyautogui
import datetime

q = datetime.datetime.now()

d= q.strftime("%Y-%m-%d %H:%M")



x = 0

while x<10:
    
    pic = pyautogui.screenshot(r'C:\Users\root\Desktop\test\qan-'+str(x)+'.png')
    
    x+=1 
how i cant add ( d ) here : qan-'+str(x)+'.png'
import os
import pyautogui
import datetime

d = datetime.datetime.now()
for i in range(10):
    file_name = os.path.join('C:/Users/root/Desktop/test', 'qan-{:%Y-%m-%d %H:%M}({}).png'.format(d, i))
    # file_name = os.path.join('C:/Users/root/Desktop/test', f'qan-{d:%Y-%m-%d %H:%M}({i}).png') # this is better, for python 3.6+
    pic = pyautogui.screenshot(file_name)
instead of os you can use pathlib module
(Aug-27-2018, 08:47 AM)buran Wrote: [ -> ]
import os
import pyautogui
import datetime

d = datetime.datetime.now()
for i in range(10):
    file_name = os.path.join('C:/Users/root/Desktop/test', 'qan-{:%Y-%m-%d %H:%M}({}).png'.format(d, i))
    # file_name = os.path.join('C:/Users/root/Desktop/test', f'qan-{d:%Y-%m-%d %H:%M}({i}).png') # this is better, for python 3.6+
    pic = pyautogui.screenshot(file_name)
instead of os you can use pathlib module

not working pro the just create one file ( qan-2018-08-27 05 ) not 10 files :)
could u please try to modify my code i need to use code that i write and understand not new one
thank u in advance
(Aug-27-2018, 09:41 AM)evilcode1 Wrote: [ -> ]not working pro the just create one file ( qan-2018-08-27 05 ) not 10 files :)
could u please try to modify my code i need to use code that i write and understand not new one
well, don't tell me it's not working.
import os
#import pyautogui
import datetime

d = datetime.datetime.now()
for i in range(10):
    file_name = os.path.join('C:/Users/root/Desktop/test', 'qan-{:%Y-%m-%d %H:%M}({}).png'.format(d, i))
    # file_name = os.path.join('C:/Users/root/Desktop/test', f'qan-{d:%Y-%m-%d %H:%M}({i}).png') # this is better, for python 3.6+
    #pic = pyautogui.screenshot(file_name)
    print(file_name)
Output:
C:/Users/root/Desktop/test/qan-2018-08-27 13:40(0).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(1).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(2).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(3).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(4).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(5).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(6).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(7).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(8).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(9).png
as you can see for yourself it produce 10 different file_names. So if it doesn't work, it's not due to changes I did. It's something with what you did. As you can see my code produce the exact file name you want, not the one you show (qan-2018-08-27 05). If I have to guess - I put my money that the problem is in the space between the date and time. But it was in your code too. You may want to change the format string to something like %Y-%m-%d_%H:%M or better %Y%m%d%H%M
Actually, I'm doing exactly what you asked - fix your code, to use the right tools and to be more pythonic.
It's generally discouraged to use string concatenation, in favor of more advanced string-formatting. One of disadvantages of concatenation is you need to cast everything that is not str to string in order to be able to concatenate. It yields ugly, non-pythonic code (sorry, but that's fact).
When you want to construct paths, use the tools that python provides. In this case string concatenation is even more discouraged.
In addition it's better to use for loop instead of while loop when you want to loop exact number of times, that you know in advance.

your code would be
import pyautogui
import datetime
 
q = datetime.datetime.now()
 
d= q.strftime("%Y-%m-%d %H:%M")
 
 
 
x = 0
 
while x<10:
     
    pic = pyautogui.screenshot(r'C:\Users\root\Desktop\test\qan-'+d + '(' + str(x)+').png')
     
    x+=1 
do you honestly think it's better?
I suggest to learn from what you are offered and ask questions if there is something you don't understand instead of stick to what you have written so far.
(Aug-27-2018, 10:56 AM)buran Wrote: [ -> ]
(Aug-27-2018, 09:41 AM)evilcode1 Wrote: [ -> ]not working pro the just create one file ( qan-2018-08-27 05 ) not 10 files :)
could u please try to modify my code i need to use code that i write and understand not new one
well, don't tell me it's not working.
import os
#import pyautogui
import datetime

d = datetime.datetime.now()
for i in range(10):
    file_name = os.path.join('C:/Users/root/Desktop/test', 'qan-{:%Y-%m-%d %H:%M}({}).png'.format(d, i))
    # file_name = os.path.join('C:/Users/root/Desktop/test', f'qan-{d:%Y-%m-%d %H:%M}({i}).png') # this is better, for python 3.6+
    #pic = pyautogui.screenshot(file_name)
    print(file_name)
Output:
C:/Users/root/Desktop/test/qan-2018-08-27 13:40(0).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(1).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(2).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(3).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(4).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(5).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(6).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(7).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(8).png C:/Users/root/Desktop/test/qan-2018-08-27 13:40(9).png
as you can see for yourself it produce 10 different file_names. So if it doesn't work, it's not due to changes I did. It's something with what you did. As you can see my code produce the exact file name you want, not the one you show (qan-2018-08-27 05). If I have to guess - I put my money that the problem is in the space between the date and time. But it was in your code too. You may want to change the format string to something like %Y-%m-%d_%H:%M or better %Y%m%d%H%M
Actually, I'm doing exactly what you asked - fix your code, to use the right tools and to be more pythonic.
It's generally discouraged to use string concatenation, in favor of more advanced string-formatting. One of disadvantages of concatenation is you need to cast everything that is not str to string in order to be able to concatenate. It yields ugly, non-pythonic code (sorry, but that's fact).
When you want to construct paths, use the tools that python provides. In this case string concatenation is even more discouraged.
In addition it's better to use for loop instead of while loop when you want to loop exact number of times, that you know in advance.

your code would be
import pyautogui
import datetime
 
q = datetime.datetime.now()
 
d= q.strftime("%Y-%m-%d %H:%M")
 
 
 
x = 0
 
while x<10:
     
    pic = pyautogui.screenshot(r'C:\Users\root\Desktop\test\qan-'+d + '(' + str(x)+').png')
     
    x+=1 
do you honestly think it's better?
I suggest to learn from what you are offered and ask questions if there is something you don't understand instead of stick to what you have written so far.

okay brother i will fallow u advice :)
but the both codes mine and ur does not work on windows 10 on linux they work okay ...
i dont know why check the video :
Oh, sorry, I should have seen this long ago
The colon (:) is not allowed in file name on windows. So it makes the file name only up to the hour (incl.). So all files have the same name and that's why you get just one at the end.

the best - make the format string %Y%m%d%H%M
(Aug-27-2018, 05:09 PM)buran Wrote: [ -> ]Oh, sorry, I should have seen this long ago
The colon (:) is not allowed in file name on windows. So it makes the file name only up to the hour (incl.). So all files have the same name and that's why you get just one at the end.

the best - make the format string %Y%m%d%H%M

working :)
thank u very much <3<3<3

okay now its time to explain for me this section :
Quote:'qan-{:%Y-%m-%d %H:%M}({}).png'.format(d, i))

can u explain it in details i need to learn i now what is (%Y-%m-%d %H:%M) for but this for what : ({}) ?
It is strange that pyautogui.screenshot() just silently ignores part of the file name. I think it should raise an error.