shaikhu-blog-v1626931446/image_d0mln9.jpg

Files, Folders and Python

Published on 2020-10-26, by Javed Shaikh

Subscribe for new article
*No spam. unsubscribe at anytime

In this post we are going to see how we can use Python to find the size of any file and folder. We will check how many files we have inside a folder and how many of them are empty files and how to delete those empty files. We can also check how old a file is and when was the last time file was updated and accessed. We are going to use Python's powerful module os for this. So lets start..

How to get current working directory and how to change it?

First of all, lets import os module.

python
1import os 2

To get the current working directory lets use getcwd() function

python
1path = os.getcwd() 2print(path) 3

Output: C:\Users\jaqsp\Desktop\other

How to change working directory?

Lets change the current working directory with chdir()

python
1os.chdir('C:\\Users\\jaqsp\\Desktop') 2

Lets check the current directory once again

python
1print(os.getcwd()) 2

Output C:\Users\jaqsp\Desktop

How to get list of all files and folders inside a directory ?

To get the list of files and folders inside a directory, we can use function scandir. This function returns an object of os.DireEntry. This object represents file path and other file attributes such as

  • name : file or directory name
  • path : full path of the file
  • is_dir() : This function returns True if the entry is a directory
  • is_file() : This function returns True if the entry is a file

So lets use scandir() function as shown below

python
1files = [] 2dirs = [] 3path = 'C:\\Users\\jaqsp\\Desktop\\other' 4with os.scandir(path) as it: 5 for file in it: 6 if os.DirEntry.is_file(file): 7 files.append(file.name) 8 else: 9 dirs.append(file.name) 10print('Directories: ',dirs) 11print('Files :',files) 12
Output : Directories: ['.ipynb_checkpoints', 'expense', 'git', 'qual-id']



files : ['exp.json', 'files.ipynb', 'Google spreadsheet.ipynb', 'gspread.ipynb', 'my_screenshot.png', 'OS.ipynb']

In the above code, we have created two empty lists files and dirs and then used scandir() function with input path. We have used is_file() and is_dir() to identify if the entry returned is file or a directory. Finally we appended the list using append function

How to get file size ?

There are two ways to get the file size i.e. using os.path.getsize() function or using os.stat() function. The size returned by each function is in bytes In below example we will get size of file exp.json

python
1size = os.path.getsize('exp.json') 2print(size) 3

Output : 2313

As shown the file size is 2313 bytes.

How to get file create, update and access date, owner name and size of file

We will use os.stat(file) function to get all the details about a file. This function returns below along with other details. We are not mentioning all the return attributes. Please check this link for all the attributes here

  • st_uid : This attribute tells the user id of file owner
  • st_gid : This is group id of file owner
  • st_size: This is size of the file in bytes
  • st_atime: Last accessed time in no of seconds
  • st_ctime: File creation time in seconds
  • st_mtime: last update/modified time in seconds

Now lets do an example

python
1stat = os.stat('exp.json') 2print(stat) 3

os.stat_result(st_mode=33206, st_ino=24206847997197434, st_dev=4237007564, st_nlink=1, st_uid=0, st_gid=0, st_size=2313, st_atime=1603293698, st_mtime=1602796599, st_ctime=1602796598)

As shown above file size is 2313 bytes and file creation/modification times are in seconds

Lets convert the time in seconds to readable string

python
1import time 2time.ctime(stat.st_ctime) 3
Output: 'Fri Oct 16 02:46:38 2020' We have used ctime function to convert the time in seconds to string readable format

How to get folder size and empty files

python
1import os 2from os.path import join, getsize 3path = 'C:\\Users\\jaqsp\\Desktop\\other' 4folder_size = 0 5empty_files = [] 6all_files = [] 7for paths,dirs,files in os.walk(path): 8 for file in files: 9 filename = os.path.join(paths, file) 10 file_size = os.path.getsize(filename) 11 all_files.append(filename) 12 if file_size == 0: 13 empty_files.append(filename) 14 folder_size += file_size 15print('current folder size is',folder_size) 16print('total files present: ',len(all_files)) 17print('Total empty files: ',len(empty_files)) 18#Print all the empty files 19print('Below are empty files found') 20for i in range(len(empty_files)): 21 print(empty_files[i]) 22

Output:

python
1current folder size is 578231 2total files present: 230 3Total empty files: 4 4Below are empty files found 5C:\Users\jaqsp\Desktop\other\qual-id\qual_id__init__.py 6C:\Users\jaqsp\Desktop\other\qual-id\test__init__.py 7C:\Users\jaqsp\Desktop\other\qual-id\test\categories__init__.py 8C:\Users\jaqsp\Desktop\other\qual-id\test\utils__init__.py 9

In the above example we have used Python's walk(path) function to list all the files inside the directory tree by walking either top-down or bottom-up Then we used join() to join the path and file and return full path of the file and finally we use getsize() function to retrieve size of the file. Folder size in above example is 578231 bytes (564KB)

How to remove a file and directory?

Deleting or removing a file or directory in Python is super easy but make sure you want to delete it as it cannot be recovered once deleted.

To remove or delete a file, Python's os module provides function remove(path). If the input path is directory, it will through an exeception as this function is only to remove a file not a folder.

python
1os.remove('abc.txt') 2

We can use rmdir(path) function to remove or delete a directory

python
1os.rmdir('some_folder') 2

Conclusion

Hope this post will help to understand how we can use Python's OS module to do daily activities related to files and directories. We learned how to get current directoy, how to change the directory and how to get different file attributes and also how we can delete a file and directory. Visit my GitHub page for code samples and don't forget to check my other posts.

References

Check my other posts

About the Author

I am a Backend System Engineer at a credit card company, specializing in C/C++ and assembler on IBM's TPF OS. I have a passion for web development and enjoy working with Node.js and Python in my free time.

Connect with author

Related articles ...

How to auto change desktop wallpaper every minute using Python

We are going to build a CLI app using Python to change desktop wallpaper every given number of minutes. Every wallpaper downloaded from internet will be unique and our app will change the wallpaper based on the time we set.

2020-10-27

How to get email alert when your website is down using shell and Python?

There are many ways to monitor and set alerts for your web server using third party apps but they don't come with free, be it Pingdom or Amazon's CloudWatch. In this post I am going to show how we can write and setup our own alert tool with just few lines of code

2020-10-29

How to setup alarm for CPU usage using IFTTT ?

Monitoring CPU and memory usage are one of the top todo checklist for a backend engineer. Sometimes you wont even notice when your server is down due to high CPU usage unless you login and manually check the system.

2020-11-04

What is fork() system call and how to fork using Python

A process in computer term is a program being executed currently by a computer. Each process is unique and can be identified by its PID or process ID.

2020-10-24