File and Directory Management in Python: 10 Essential Operations
Python developers don’t need to open their file explorers
Python is one of the best programming languages to automate the boring stuff about system management. For example, if you maintain a website and need to remove some out-of-date log files on some directories every day. How to do it?
A non-programmer will remove these useless files one by one.
A Linux guru will write a Shell script and schedule a task.
A Python developer, even if he doesn’t know anything about Shell scripts, can also automate the task with Python. After all, when it comes to file and directory management, Python can do everything that Linux Shell or Windows PowerShell scripts do.
This article will dive into the 10 most common operations about file and directory management in Python. After reading, you probably don’t need to open your file explorer again. Or you don’t even need a graphical user interface (GUI) to handle your computer. After all, cool hackers in films never use GUI. 😄
1. Print Current Working Directory
Before we do something on our system, the first information we need to know is where we are. In Python, we can easily get the current working directory by the os.getcwd() method:
>>> import os
>>> os.getcwd()
# '/Users/yangsmac/PycharmProjects/pythonProject'Besides, if we are using Python 3.4 and above, another module called Path can help us as well:
>>> from pathlib import Path
>>> Path.cwd()
# PosixPath('/Users/yangsmac/PycharmProjects/pythonProject')2. Change Current Working Directory
If we are not on the right place, it’s also easy to change the current directory:
>>> os.chdir('/Users')
>>> os.getcwd()
# '/Users'3. List All Files and Directories
After entered the expected directory, a good habit is to check out all the files and directories before doing something here. Cause if the files or directories are not what we expected, we probably entered the wrong path.
>>> os.listdir()
['yangsmac', '.localized', 'Shared']4. Determine Whether a File Exists
Sometimes, one directory has too many files. We probably don’t want to wait too much time to list all of them, especially when we just need to touch a few files. In such cases, we can just check one file or directory exists or not:
>>> os.path.exists('yangsmac')
True
>>> os.path.isdir('yangsmac')
True
>>> os.path.isfile('yangsmac')
FalseBesides the traditional os module, the Path module can do this job as well:
>>> Path('yangsmac').exists()
True
>>> Path('yangsmac').is_file()
False
>>> Path('yangsmac').is_dir()
True5. Get Information of a File
After knowing the existence of a file, we can move forward to check more information of it:
>>> t = Path('test1.py')>>> t.stat()
os.stat_result(st_mode=33188, st_ino=1026971, st_dev=16777231, st_nlink=1, st_uid=501, st_gid=20, st_size=72, st_atime=1634498556, st_mtime=1634498555, st_ctime=1634498555)>>> t.name
'test1.py'>>> t.suffix
'.py'>>> t.stem
'test1'As shown above, the Path module is super useful and flexible to check more details of a file, such as the size of the file (st_size), time of the last modification (st_mtime) and so on. (We can check the official document to understand all the meanings of the parameters.)
And of course, the os module can do this as well:
os.stat('test1.py')6. Create a New File or Directory
Thanks to the os module, creating new stuff is super simple in Python. Such as creating a new directory:
os.mkdir('yang')And to create a new file:
os.mknod('yang.txt')7. Rename a File or Directory
The os.rename() function can help us to rename things easily:
>>> os.listdir()
['yang']>>> os.rename('yang','legend')>>> os.listdir()
['legend']As we mentioned before, to avoid making mistakes, it’s a good habit to list the files before we change something.
8. Remove a File or Directory
To remove a file with Python, the os module is useful enough:
os.remove('yang.txt')However, to remove a directory is a little complex. If a directory is empty, everything is fine:
os.rmdir('yang')Unfortunately, the
os.rmdir()method can only remove empty directories.
To remove a non-empty directory, we can use the rmtree() method inside the shutil module.
>>> import shutil
>>> shutil.rmtree('yang')9. Move or Copy a File
In fact, the shutil module we just used is another helpful tool for system management in Python. It’s the best friend of the os module. 🙂
For example, if we need to move a file, the shutil.move() method makes everything simple:





