Search code examples
pythonpython-3.xfiletextauto-generate

Generate a filename in python


I feel like this question was answered many times before, but i can't find anything.

I have a script which takes a text as command line argument and then generates images with the text in it. It generates image for every word enterd in to the command line but I don't know how to save them. I want to save them like this; first image would be saved as 0001.png, the second one as 0002.png and so on, how can I do this?


Solution

  • Use f-strings, you can specify how many 0's the digit should have at minimum:

    for i in range(1, 1200, 103): # Just to generate bigger numbers
        print(f"{i:04d}.png")
    

    Output:

    0001.png
    0104.png
    0207.png
    0310.png
    0413.png
    0516.png
    0619.png
    0722.png
    0825.png
    0928.png
    1031.png
    1134.png