0

I have a loop to add filenames in a Python script. When the number of files reach 5000+, this is causing high CPU usage.

Is there any way to avoid this or make this more efficient?

number = 1
while True:
    fname = "Capture_" + str(number) + ".jpg"
    if not os.path.exists(fname):
        break
    number = number + 1

DoSomething(fname)

Ok. Based on the comments, i came up with this code. If i only did max(glob.glob('Capture_*.jpg')) then i only get Capture_999.jpg

if os.path.exists('Capture_1.jpg'):
    number = max(glob.glob('Capture*'), key=os.path.getctime).replace('Capture_', '').replace('.jpg', '')
    number = str(int(number) + 1)
else:
    number = '1'

fname = 'Capture_' + number + '.jpg'

But it seems to be again causing high CPU usage. Is it possible to make this more efficient?

I finally resolved with this simply with : fname = 'Capture_' + str(len(os.listdir(Path)) + 1) + '.jpg'

Naveen
  • 770
  • 10
  • 22
  • 1
    From linked question, `max(glob.glob('Capture_*.jpg'))` should give you the filename with the max number. Then you just need to extract the number from the string to get `number` if you need it. – raul.vila Apr 20 '18 at 13:56
  • 1
    Your loop will use as much CPU as it can. I suspect it's just more apparent when it runs for longer. What you are doing seems very inefficient, though. Can't you just count the files in the directory and then assume the next nummber (after checking it doesn't already exist, of course)? – George Kettleborough Apr 20 '18 at 13:56
  • This script is not trying to get the name of the files in a folder. This script IS naming the files created. And at the end of every day, the number of files grow huge and these checks use high CPU. – Naveen Apr 20 '18 at 13:56
  • < This script IS naming the files created. > I don't see that. `if not os.path.exists(fname)` is checking if the file exists. – raul.vila Apr 20 '18 at 13:57
  • If i Count the number of files in the folder everytime, doesnt it do a similar thing as this? – Naveen Apr 20 '18 at 13:58
  • understood now. I can get the max filename and then create a new filename. Marking as duplicate. – Naveen Apr 20 '18 at 14:00
  • < If i Count the number of files in the folder ... > If you only have files named `Capture_##.jpg` and there is no gaps yes, if not you'll need to filter them as I suggested with `glob.glob('Capture_*.jpg')`. I provided a suggestion which works in the more general case with the info I had. – raul.vila Apr 20 '18 at 14:00

0 Answers0