I am having trouble writing ascending numbered files that keep on ticking after reboot of system. I have no prior experience in this

I've tried following this guide: Python: How do I create sequential file names?

He recommends checking dir like this:

import os
def numbers( path ):
    for filename in os.listdir(path):
        name, _ = os.path.splitext()
        yield int(name[4:])
count = max( numbers( '/path/to/files' ) )

count += 1

I tried writing it to my code like this:

import os

picPath = "/home/pi/Desktop/images/"

def numbers( picPath ):
    for filename in os.listdir(picPath):
        name, _ = os.path.splitext()
        yield int(name[4:])
count = max( numbers( picPath ) )

count += 1

I want to be able to reboot the RPi and continue saving image files in ascending numbering order

Thank you very much for your help! Hit me up for any clarifying questions

Edit: I am expecting up to 2000 image files 1000x1000 pixel around 200kB. If there is a faster way to do this without checking the whole directory, i am very interested!

  • Is there a problem or do you just want a faster method? You could just get the last modified file in the directory and increase the number. How to get the latest file in a folder using python – Johnny Mopp Jun 7 at 10:15
  • Thank you for your question! It doesnt work, and i don't understand why. I will check your link. Sounds like a good idea to check the last modified file – Grumsk Jun 7 at 10:18
  • Also, You need to pass a parameter: name, _ = os.path.splitext(filename). – Johnny Mopp Jun 7 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.