Get Name, Size, Type & Date of Files in Python
This article is all about files and getting data about their features in Python. Before we start gathering data about the files we will look at a function to list a directory’s contents.
Then we will gather data from the found contents, to find out: what are the names, types, sizes and creation- and change dates of the found files?

How to get a list of the files and folders in a directory?
The listdir function from the os package returns a list with the contents of a directory.
You can specify the directory that you want to search by passing its filepath to the function. If you don’t specify a filepath the function uses the current working directory.
To learn more about filepaths in Python check out this article:
Here is an example of using the listdir function:
import os
directory_path = 'C:/Users/BE/Documents/Special Project'
contents = os.listdir(directory_path)
print(contents)This is what it prints:
['apple.jpg', 'cards.jpg', 'chatting robot.png', 'construction worker.jpg',
'dog.jpg', 'laptop.jpg', 'notes.txt', 'open book.jpg',
'people taking a picture.jpg', 'python.jpg', 'python.png', 'rooster.jpg',
'Specials', 'working on computer.jpg', 'working on laptop.png',
'working_on_a_computer.jpg']As you can see, it is a list with the names of the files that are in the directory. There also is a directory/folder called Specials.
How to get the size of files?
In Python we can use the stat function from the os package to get data about a file. One of the stats provided by the function is the size in bytes, this will be stored in st_size.
We will specify a filepath to the stat function. It will return an object containing the statistics. And then we extract the st_size value from that object.
directory_path = 'C:/Users/BE/Documents/Special Project'
filename = 'dog.jpg'
filepath = directory_path + '/' + filename
stats = os.stat(filepath)
size = stats.st_size
print(size)This is what it prints: 81113, so the size of the dog.jpg file is 81113 bytes.
How to get the dates of files?
A file can have different datetimes associated with it and we can get them with the same stat function we just used to get the size:
- the datetime the file was created, stored in
st_ctime - the datetime the file was last modified, stored in
st_mtime - the datetime the file was last accessed, stored in
st_atime
These work on Windows:
import os
directory_path = 'C:/Users/BE/Documents/Special Project'
filename = 'dog.jpg'
filepath = directory_path + '/' + filename
stats = os.stat(filepath)
creation_datetime = stats.st_ctime
print(creation_datetime)
last_change_datetime = stats.st_mtime
print(last_change_datetime)
last_accessed_datetime = stats.st_atime
print(last_accessed_datetime)This is what it prints:
1676276165.7291226
1676276989.9845095
1676277534.316749These are datetimes noted in the number of seconds elapsed since midnight on January 1, 1970 midnight in UTC time. We can convert them to human readable datetime (in system’s timezone) with the fromtimestamp method from the datetime package’s datetime class:
from datetime import datetime
print(datetime.fromtimestamp(creation_datetime))This is what it prints: 2023–02–13 09:16:05.646298
To get the creation time on Mac you can use: st_birthtime instead of st_ctime.
How to get the type of a file?
We will determine the type of the files by looking at the file extensions. We can use the splitext function to split the extension from the filename or filepath.
Here is how that works:
import os
directory_path = 'C:/Users/BE/Documents/Special Project'
filename = 'dog.jpg'
filepath = directory_path + '/' + filename
extension = os.path.splitext(filepath)[1]
print(extension)This is what it prints: .jpg.
The splitext correctly splits the filepath in 2 parts, the filepath/filename and the file extension. It returns both in a tuple, so if we take index 1 form that tuple we get the file extension.
Thank you for reading!
You can get full access to all my posts by joining Medium. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:
You might also like:






