Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I determine an open file's size in Python?

There's a file that I would like to make sure does not grow larger than 2 GB (as it must run on a system that uses ext 2). What's a good way to check a file's size bearing in mind that I will be writing to this file in between checks? In particular, do I need to worry about buffered, unflushed changes that haven't been written to disk yet?

like image 292
Jason Baker Avatar asked Dec 08 '09 14:12

Jason Baker


People also ask

How do you check the size of a file using Python?

Use os.path.getsize() function Use the os. path. getsize('file_path') function to check the file size. Pass the file name or file path to this function as an argument.

How do I find the size of a csv file in Python?

Using stat() from the os module, you can get the details of a file. Use the st_size attribute of stat() method to get the file size. The unit of the file size is byte .

How do I find the size of a file path?

Get file size in java using FileChannel class We can use FileChannel size() method to get file size in bytes.


1 Answers

Perhaps not what you want, but I'll suggest it anyway.

import os
a = os.path.getsize("C:/TestFolder/Input/1.avi")

Alternatively for an opened file you can use the fstat function, which can be used on an opened file. It takes an integer file handle, not a file object, so you have to use the fileno method on the file object:

a = open("C:/TestFolder/Input/1.avi")
b = os.fstat(a.fileno()).st_size
like image 164
Dominic Bou-Samra Avatar answered Oct 07 '22 01:10

Dominic Bou-Samra