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.
Related Posts:
- How do I copy a file in Python?
- How do I copy a file in Python?
- How to open a file using the open with statement
- Using “with open() as file” method, how to write more than once? [duplicate]
- How to read a file line-by-line into a list?
- TypeError: a bytes-like object is required, not ‘str’ when writing to a file in Python3
- Confused by python file mode “w+”
- File open and close in python
- Do I understand os.walk right?
- FileNotFoundError: [Errno 2] No such file or directory [duplicate]
- How to move a file in Python?
- Python open() gives FileNotFoundError/IOError: Errno 2 No such file or directory
- FileNotFoundError: [Errno 2] No such file or directory
- How to move a file in Python?
- How do you append to a file?
- Writing a list to a file with Python
- Why do I get “Pickle – EOFError: Ran out of input” reading an empty file?
- How do I copy a file in Python?
- How do I check whether a file exists without exceptions?
- Call a function from another file?
- Writing a list to a file with Python
- Python list directory, subdirectory, and files
- Where does this come from: -*- coding: utf-8 -*-
- Open file in a relative location in Python
- Why am I getting a FileNotFoundError?
- Python: Write array values into file
- Importing variables from another file?
- How to copy files?
- Flask raises TemplateNotFound error even though template file exists
- importing external “.txt” file in python
- Search and replace a line in a file in Python
- How to save a dictionary to a file?
- How to open a file for both reading and writing?
- How do you read a file into a list in Python?
- SSL error : routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
- What is the perfect counterpart in Python for “while not EOF”
- .write not working in Python
- Creating files and directories via Python
- How to convert a file into a dictionary?
- How to delete a specific line in a file?
- Python read in string from file and split it into values
- How often does python flush to a file?
- How to open every file in a folder
- TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
- Loading and parsing a JSON file with multiple JSON objects
- How to find out whether a file is at its `eof`?
- Python IOError: File not open for reading
- What does %s mean in a python format string?
- ImportError: No module named ‘tensorflow.python’
- How to access environment variable values
- python numpy ValueError: operands could not be broadcast together with shapes
- error: command ‘gcc’ failed with exit status 1 while installing eventlet
- Python foreach equivalent
- Python NoneType object is not callable (beginner)
- _tkinter.TclError: no display name and no $DISPLAY environment variable
- What does if __name__ == “__main__”: do?
- How to fix IndexError: invalid index to scalar variable
- How to write inline if statement for print?
- Could not install packages due to a “Environment error :[error 13]: permission denied : ‘usr/local/bin/f2py'”
- Why are Python’s ‘private’ methods not actually private?
- Pandas DataFrame column to list
- Error: ” ‘dict’ object has no attribute ‘iteritems’ “
- Does Python have “private” variables in classes?
- Rename Pandas DataFrame Index
- How to upgrade pip3?
- Making a collatz program automate the boring stuff
- Reverse / invert a dictionary mapping
- using pip3: module “importlib._bootstrap” has no attribute “SourceFileLoader”
- TypeError: unsupported operand type(s) for -: ‘list’ and ‘list’
- What’s the difference between a Python module and a Python package?
- How to overcome “datetime.datetime not JSON serializable”?
- AttributeError: ‘datetime’ module has no attribute ‘strptime’
- SyntaxError invalid token
- TypeError: ‘type’ object is not iterable – Iterating over object instances
- How to check the version of scipy
- How to check if type of a variable is string?
- How to print colored text to the terminal
- ImportError: No module named win32com.client
- Error message “Linter pylint is not installed”
- AttributeError: ‘Tensor’ object has no attribute ‘_keras_history’
- What is the quickest way to HTTP GET in Python?
- Pandas index column title or name
- Removing nan values from an array
- Python – While-Loop until list is empty
- ImportError: No module named xgboost
- How to properly create a HeatMap with Bokeh
- How to detect key presses?
- NaN loss when training regression network
- How do I merge lists in python? [duplicate]
- creating a reverse method for a python list from scratch
- What does on_delete do on Django models?
- Convert floats to ints in Pandas?
- Unable to import tweepy module
- pandas comparison raises TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]
- How can I recover the return value of a function passed to multiprocessing.Process?
- Python Remove last char from string and return it
- NameError: name ‘reduce’ is not defined in Python
- numpy : calculate the derivative of the softmax function
- Difference between exit(0) and exit(1) in Python
- IOError: [Errno 32] Broken pipe when piping: `prog.py | othercmd`