Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python os.stat(file_name).st_size versus os.path.getsize(file_name)

I've got two pieces of code that are both meant to do the same thing -- sit in a loop until a file is done being written to. They are both mainly used for files coming in via FTP/SCP.

One version of the code does it using os.stat()[stat.ST_SIZE]:

size1,size2 = 1,0
while size1 != size2:
  size1 = os.stat(file_name)[stat.ST_SIZE]
  time.sleep(300)
  size2 = os.stat(file_name)[stat.ST_SIZE]

Another version does it with os.path.getsize():

size1,size2 = 0,0
while True:
  size2 = os.path.getsize(file_name)
  if size1 == size2:
    break
  else:
    time.sleep(300)
    size1 = size2

I've seen multiple instances where using the first method reports that the sizes are the same while the file is actually still growing. Is there some underlying reason why os.stat() would incorrectly report while os.path.getsize() would not? I'm not seeing any errors or exceptions come back.

like image 530
Valdogg21 Avatar asked Sep 23 '13 14:09

Valdogg21


People also ask

What is os path Getsize in Python?

path. getsize() method in Python is used to check the size of specified path. It returns the size of specified path in bytes. The method raise OSError if the file does not exist or is somehow inaccessible.

What is Python os path?

The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats.

What is os path Islink?

islink() method in Python is used to check whether the given path represents an existing directory entry that is a symbolic link or not. Note: If symbolic links are not supported by the Python runtime then os.

What does os path Sep do?

os. path. sep is the character used by the operating system to separate pathname components.


1 Answers

In CPython 2.6 and 2.7, os.path.getsize() is implemented as follows:

def getsize(filename):
    """Return the size of a file, reported by os.stat()."""
    return os.stat(filename).st_size

From this, it seems pretty clear that there is no reason to expect the two approaches to behave differently (except perhaps due to the different structures of the loops in your code).

like image 189
NPE Avatar answered Oct 23 '22 21:10

NPE