Python Forum
datetime with every screenshot name
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
datetime with every screenshot name
#4
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
datetime with every screenshot name - by evilcode1 - Aug-27-2018, 08:38 AM
RE: datetime with every screenshot name - by buran - Aug-27-2018, 08:47 AM
RE: datetime with every screenshot name - by buran - Aug-27-2018, 10:56 AM
RE: datetime with every screenshot name - by buran - Aug-27-2018, 05:09 PM
RE: datetime with every screenshot name - by buran - Aug-27-2018, 06:01 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  IEDriverServer screenshot ABVSVL 0 1,774 Jul-12-2020, 09:31 AM
Last Post: ABVSVL
  pyautogui.screenshot region is not working alexlu 6 6,619 Jun-04-2020, 10:46 AM
Last Post: alexlu
  What is the best way to search for a pixel in a screenshot? TheZadok42 1 2,689 May-15-2020, 12:37 PM
Last Post: scidam
  Want to take a Screenshot from a File in Linux dhiliptcs 2 2,604 Oct-21-2019, 01:22 PM
Last Post: dhiliptcs
  TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str' findbikash 2 9,714 Sep-18-2019, 08:32 AM
Last Post: buran
  Screenshot of specific window kainev 10 19,596 Nov-30-2018, 03:07 PM
Last Post: kainev
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 9,316 Aug-06-2018, 05:19 PM
Last Post: vanicci
  [Help] sorted() in while loop with user's input() {Screenshot attached} vanicci 5 4,090 Aug-04-2018, 08:59 PM
Last Post: vanicci
Question [Help] How to end While Loop using counter? {Screenshot attached} vanicci 2 3,122 Aug-02-2018, 10:09 PM
Last Post: vanicci

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020