avatarBetter Everything

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3711

Abstract

filepath to the <code>stat</code> function. It will return an object containing the statistics. And then we extract the <code>st_size</code> value from that object.</p><div id="5b63"><pre>directory_path = <span class="hljs-string">'C:/Users/BE/Documents/Special Project'</span> filename = <span class="hljs-string">'dog.jpg'</span>

filepath = directory_path + <span class="hljs-string">'/'</span> + filename stats = os.stat(filepath) size = stats.st_size <span class="hljs-built_in">print</span>(size)</pre></div><p id="17fa">This is what it prints: <code>81113</code>, so the size of the <code>dog.jpg</code> file is 81113 bytes.</p><h2 id="5127">How to get the dates of files?</h2><p id="c679">A file can have different datetimes associated with it and we can get them with the same <code>stat</code> function we just used to get the size:</p><ul><li>the datetime the file was created, stored in <code>st_ctime</code></li><li>the datetime the file was last modified, stored in <code>st_mtime</code></li><li>the datetime the file was last accessed, stored in <code>st_atime</code></li></ul><p id="835b">These work on <b>Windows</b>:</p><div id="40fc"><pre><span class="hljs-keyword">import</span> os

directory_path = <span class="hljs-string">'C:/Users/BE/Documents/Special Project'</span> filename = <span class="hljs-string">'dog.jpg'</span>

filepath = directory_path + <span class="hljs-string">'/'</span> + filename stats = os.stat(filepath)

creation_datetime = stats.st_ctime <span class="hljs-built_in">print</span>(creation_datetime)

last_change_datetime = stats.st_mtime <span class="hljs-built_in">print</span>(last_change_datetime)

last_accessed_datetime = stats.st_atime <span class="hljs-built_in">print</span>(last_accessed_datetime)</pre></div><p id="3ee5">This is what it prints:</p><div id="630f"><pre><span class="hljs-number">1676276165.7291226</span> <span class="hljs-number">1676276989.9845095</span> <span class="hljs-number">1676277534.316749</span></pre></div><p id="4334">These 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 <code>fromtimestamp</code> method from the <code>datetime</code> package’s <code>datetime</code> class:</p><div id="8350"><pre><span class="hljs-keyword">from</span> datetime <span class="hljs-keyword">import</span> datetime <span class="hljs-built_in">print</span>(datetime.fromtimestamp(creation_datetime))</pre></div><p id="05b8">This is what it prints: <code>2023–02–13 09:16:05.646298</code></p><p id="c8f6">To get the creation time on Mac you can use: <code>st_birthtime</code> instead of <code>st_ctime</code>.</p><h2 id="c39b">How to get the type of a file?</h2><p id="f038">We will determine the type of the files by looking at the file extensions. We can use the <code>splitext</code> function to split the extension from the filename or filepath.</p><p id="09c8">Here is how that works:</p><div id="b150"><pre><span class="hljs-keyword">import</span> os

directory_path = <span class="hljs-string">'C:/Users/BE/Documents/Special Project'</span> filename = <span class="hljs-string">'dog.jpg'</span>

filepath = directory_path + <span class="hljs-string">'/'</span> + filename extension = os.path.splitext(filepath)[<span class="hljs-number">1</span>] <span class="hljs-built_in">print</span>(extension)</pre></div><p id="bd3a">This is what it prints: <code>.jpg</code>.</p><p id="ae04">The <code>splitext</code> 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.</p><h1 id

Options

="f0bc">Thank you for reading!</h1><p id="8ee0">You can <b>get full access to all my posts by joining Medium</b>. Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium:</p><div id="80fc" class="link-block"> <a href="https://medium.com/@BetterEverything/membership"> <div> <div> <h2>Join Medium with my referral link — Better Everything</h2> <div><h3>Read every story from Better Everything (and thousands of other writers on Medium). Your membership fee directly…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*aa4Y_6MHVoY6Wl-9)"></div> </div> </div> </a> </div><p id="3978">You might also like:</p><div id="bdf4" class="link-block"> <a href="https://readmedium.com/automate-incoming-file-processing-with-python-df7daca0e7ff"> <div> <div> <h2>Automate Incoming File Processing with Python</h2> <div><h3>I will show you how you can automatically process files after they enter a certain file folder with a Python directory…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*qUpNn-d9WLM69Wkz8KIugA.jpeg)"></div> </div> </div> </a> </div><div id="f978" class="link-block"> <a href="https://readmedium.com/automate-removal-of-old-files-in-python-2085381fdf51"> <div> <div> <h2>Automate Removal of Old Files in Python</h2> <div><h3>I explain how to build a Python program that checks if the files in a directory are older than a threshold and if so…</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*fwDsvAbuFKi3f1zVnLRt9g.png)"></div> </div> </div> </a> </div><div id="bb9e" class="link-block"> <a href="https://readmedium.com/passing-data-to-a-python-file-when-running-it-with-commands-3cc437667b2b"> <div> <div> <h2>Passing Data to a Python File when Running It with Commands</h2> <div><h3>Feed input data into your Python script by using command line arguments and collecting them in your Python program.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*lhn4RrOGYuv3mSc0D4f_zg.jpeg)"></div> </div> </div> </a> </div><div id="2dbb" class="link-block"> <a href="https://readmedium.com/move-rename-delete-files-in-python-e3c7b7967a02"> <div> <div> <h2>Move, Rename & Delete Files in Python</h2> <div><h3>In this article I will show you 3 useful operations for managing files in Python: moving, renaming & deleting a file.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*ellO2TvMYdFzROd-p30SKw.png)"></div> </div> </div> </a> </div></article></body>

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?

With Python you can analyze the contents of a directory and find the files’ names, types, sizes and dates. Image by catalyststuff on Freepik

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.316749

These 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:

Python
Programming
Software Development
Automation
Data Engineering
Recommended from ReadMedium