Getting File Size in Python

How do I check file size in Python?

You need the st_size property of the object returned by os.stat. You can get it by either using pathlib (Python 3.4+):

>>> from pathlib import Path
>>> Path('somefile.txt').stat()
os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400)
>>> Path('somefile.txt').stat().st_size
1564

or using os.stat:

>>> import os
>>> os.stat('somefile.txt')
os.stat_result(st_mode=33188, st_ino=6419862, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=1564, st_atime=1584299303, st_mtime=1584299400, st_ctime=1584299400)
>>> os.stat('somefile.txt').st_size
1564

Output is in bytes.

how to get file size without downloading it in python requests

Instead of using GET request, do HEAD request:

resp = requests.request('HEAD', "https://Whereever.user.wants.com/THEFILE.zip")

The HTTP HEAD method requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method. In your case, where URL produces a large download, a HEAD request would read its Content-Length header to get the filesize without actually downloading the file.

Whats the best way to get the filesize?

The first method would be a waste if you don't need the contents of the file anyway. Either of your other two options are fine. os.path.getsize() uses os.stat()

From genericpath.py

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

Edit:

In case it isn't obvious, os.path.getsize() comes from genericpath.py.

>>> os.path.getsize.__code__
<code object getsize at 0x1d457b0, file "/usr/lib/python2.7/genericpath.py", line 47>

Getting file size before post with Python

This might help if you want to check size details before saving :

@app.route('/api/uploadJob', methods = ['GET', 'POST'])
def uolpadJob():
try:
if request.method == 'POST':
f = request.files['file']
fullFilePath = os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(f.filename))
f.seek(0, 2)
file_length = f.tell()
# Introduce your disk space condition and save on basis of that
f.save(fullFilePath)

However if you want to checkup after saving the file to your designated path, then try this :

@app.route('/api/uploadJob', methods = ['GET', 'POST'])
def uolpadJob():
try:
if request.method == 'POST':
f = request.files['file']
fullFilePath = os.path.join(app.config['UPLOAD_FOLDER'],
secure_filename(f.filename))
f.save(fullFilePath)
fileSize = os.stat(fullFilePath).st_size

can't get a file size in python

It is failing because of the line os.chdir(mypath). You don't need to chdir().

Assuming the path is correct and the file exists, it should work (print the file size) if you remove the os.chdir() statement.

how to get the size of csv file line by line?is thas possible using python

Something along the lines of this should work for getting the bytes per row/line:

(The +1 is for the newline)

import os

total_bytes = -1

with open("test.csv") as file_in:
for line in file_in:
bytes_on_this_line = len(line) + 1
total_bytes += bytes_on_this_line

print(total_bytes)

print(os.path.getsize("test.csv"))

Output:

15
15

Get file size, creation date and modification date in Python

https://docs.python.org/2.7/library/os.path.html#module-os.path

os.path.getsize(path) # size in bytes
os.path.ctime(path) # time of last metadata change; it's a bit OS specific.

Here's a rewrite of your program. I did this:

  1. Reformatted with autopep8 for better readability. (That's something you can install to prettify your code your code. But IDEs such as PyCharm Community Edition can help you to do the same, in addition to helping you with code completion and a GUI debugger.)
  2. Made your getListofFiles() return a list of tuples. There are three elements in each one; the filename, the size, and the timestamp of the file, which appears to be what's known as an epoch time (time in seconds since 1970; you will have to go through python documentation on dates and times).
  3. The tuples is written to your text file in a .csv style format (but note there are modules to do the same in a much better way).

Rewritten code:

import os

def getListOfFiles(ruta):
listOfFile = os.listdir(ruta)
allFiles = list()
for entry in listOfFile:
fullPath = os.path.join(ruta, entry)
if os.path.isdir(fullPath):
allFiles = allFiles + getListOfFiles(fullPath)
else:
print('getting size of fullPath: ' + fullPath)
size = os.path.getsize(fullPath)
ctime = os.path.getctime(fullPath)
item = (fullPath, size, ctime)
allFiles.append(item)
return allFiles

ruta = "FolderPath"
miArchivo = open("TxtPath", "w")
listOfFiles = getListOfFiles(ruta)
for elem in listOfFiles:
miArchivo.write("%s,%s,%s\n" % (elem[0], elem[1], elem[2]))
miArchivo.close()

Now it does this.

my-MBP:verynew macbookuser$ python verynew.py; cat TxtPath
getting size of fullPath: FolderPath/dir2/file2
getting size of fullPath: FolderPath/dir2/file1
getting size of fullPath: FolderPath/dir1/file1
FolderPath/dir2/file2,3,1583242888.4
FolderPath/dir2/file1,1,1583242490.17
FolderPath/dir1/file1,1,1583242490.17
my-MBP:verynew macbookuser$


Related Topics



Leave a reply



Submit