Ross
Ross

Reputation: 13

Python file size without opening file

Do both os.stat and os.path.getsize open and close the file to get this information? Is there a faster way to obtain the file size for large amounts of data without opening each file when I'm scanning the contents of terabytes of files?

Upvotes: 1

Views: 1146

Answers (1)

heemayl
heemayl

Reputation: 42127

Assuming Linux, filesize is stored on the metadata on the file (precisely in the inode of the relevant file, maintained by the filesystem).

So, you don't need to open the file to get the file size, and os.stat and similar methods does not open the file to get the file size obviously. They simply do the stat(2) (and similar) system call underneath to read the inode data.

Upvotes: 1

Related Questions