0

I'm looking to make memory improvements. Is there a better way to check if a file is empty besides what I currently have (checking if filepath exists and size is zero bytes)?

if os.path.exists(file_path) and os.stat(file_path).st_size == 0

bnykpp
  • 55
  • 1
  • 5
  • 2
    that shouldn't take a lot of memory. Why do you think this the problem is in this line? – Marat Jul 06 '23 at 00:45
  • 2
    Why not using ```os.path.getsize()``` function to directly retrieves the file size without accessing the complete metadata. – Daniel Hao Jul 06 '23 at 00:49
  • 1
    I'm not familiar with memory management so I want to test different options out. Will try os.path.getsize(). Thanks! – bnykpp Jul 06 '23 at 01:24
  • 1
    @DanielHao That does the same thing: https://github.com/python/cpython/blob/13aefd175e3c04529251f175c23cb3ed88451fd0/Lib/genericpath.py#L60 – SuperStormer Jul 06 '23 at 02:13

0 Answers0