Get Size of File Without Reading in Python
The Python standard library offers developers an piece of cake toolkit for getting associated file information. Getting the size of a file in Python is easily done via the Os module's stat method. Here we'll take a quick look at this method, how information technology'south used, and what i needs to exist aware of.
- 1 Creating a Sample Text File
- 2 os.path.getsize()
- 3 os.stat()
- 4 Converting Bytes into Kilobytes & Megabytes
- 5 Some Considerations
- vi Final Thoughts
Creating a Sample Text File
To get started with the os.stat()
method, let's create a sample file to work within the current working directory. This manner, we'll exist on the same page as we work through getting the size of a file using the os module. The post-obit lawmaking volition create a text file named sample.txt with the word alpharithms written 256 times separated by the \n
newline character:
# Create a sample file in the local working directory with open('sample.txt', 'west')every bit file: file.write("alpharithms\northward" * 256) # Results in sample.txt alpharithms alpharithms alpharithms ... alpharithms <------ 256th line
Python makes getting the size of a file incredibly piece of cake. At that place are 2 primary ways to reach this with standard library methods—both of which use the bone module. The kickoff method relies on the os.path
module and the second on the bone.stat
module. The stat module provides more detailed information but the os.path.getsize()
method is more concise. Let'southward take a wait at how to go file size in Python using both.
bone.path.getsize()
The getsize() method takes a path-like object (cord, in the example below) every bit an argument. This method will return an integer value representing the size of the file. Nosotros tin can implement this as shown in the code below:
# Become the size of the file size = os.path.getsize("sample.txt") >>> size 3328
os.stat()
The os.stat module provides a st_size attribute that informs the developer of the size of a file, once again in bytes. This is achieved, referencing the previously-created sample file, in the post-obit single line of lawmaking:
# Become the size of the file size = os.stat("sample.txt").st_size >>> size 3328
Converting Bytes into Kilobytes & Megabytes
So at present we know that our file is of size 3328—but what exactly does that tell united states of america? Information technology certainly isn't 3328 characters—our file had only 3072. The .st_size
attribute of an os.stat()
object returns the file size in bytes.
Checking a file'south size in bytes is useful in some cases. Other times, units like kilobytes (kb) or megabytes (MB) are preferred. Fortunately, knowing that 1024 bytes make a kilobyte and that 1024 kilobytes make a megabyte, we can practice some easy conversions:
# Get the size of the file size = bone.stat("sample.txt").st_size # Catechumen to familiar units kb = size / 1024 mb = size / 1024 / 1024 # print the results to the panel print("size in kb:", kb) impress("size in mb:", MB) # results size in kb: 3.25 size in mb: 0.003173828125
Some Considerations
The os.stat
method is equivalent to os.f_stat()
in Python 3.3+. This method now takes an integer value as an argument—referencing a file descriptor—and will throw an TypeError
if passed a file path such every bit sample.txt
as an statement. It'due south worth noting that the bone.stat()
method can accept both file descriptor objects as well as strings representing a file path.
For Windows users, the bone.stat()
method can ignore file pointer chaining by passing forth the keyword argument follow_symlinks=Simulated
which is True
by default. In such cases, Python will at present use the specified path rather than raising an mistake. Check the official documentation for more details.
Concluding Thoughts
Getting the size of a file in Python is critical to many I/O operations. Among the many uses, knowing the size of files can likewise be used to aid track progress during operations such as downloads, uploads, or writing to disk. Both the os.stat
and os.path.getsize
approaches here will go the chore done and are provided via the standard Python library. Hard to get any simpler or efficient than that!
Source: https://www.alpharithms.com/python-get-file-size-271811/
Post a Comment for "Get Size of File Without Reading in Python"