Published on 2020-10-26, by Javed Shaikh
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..
First of all, lets import os module.
1import os
2
To get the current working directory lets use getcwd() function
1path = os.getcwd()
2print(path)
3
Output: C:\Users\jaqsp\Desktop\other
Lets change the current working directory with chdir()
1os.chdir('C:\\Users\\jaqsp\\Desktop')
2
Lets check the current directory once again
1print(os.getcwd())
2
Output C:\Users\jaqsp\Desktop
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
So lets use scandir() function as shown below
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
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
1size = os.path.getsize('exp.json')
2print(size)
3
Output : 2313
As shown the file size is 2313 bytes.
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
Now lets do an example
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
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
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:
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)
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.
1os.remove('abc.txt')
2
We can use rmdir(path) function to remove or delete a directory
1os.rmdir('some_folder')
2
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.
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
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
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
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