avatarYang Zhou

Summary

This article discusses 10 essential file and directory management operations in Python, including printing the current working directory, changing the current directory, listing all files and directories, determining whether a file exists, getting information about a file, creating a new file or directory, renaming a file or directory, removing a file or directory, moving or copying a file, and zipping and unzipping files.

Abstract

Python is a powerful programming language that can automate system management tasks, such as file and directory management. This article discusses 10 essential operations for managing files and directories in Python using the os, Path, and shutil modules. The operations include printing the current working directory, changing the current directory, listing all files and directories, determining whether a file exists, getting information about a file, creating a new file or directory, renaming a file or directory, removing a file or directory, moving or copying a file, and zipping and unzipping files. These operations can help Python developers automate tasks, such as removing out-of-date log files from a website, and eliminate the need for manual file management.

Bullet points

  • Python can automate system management tasks, including file and directory management.
  • The os module provides methods for printing the current working directory, changing the current directory, listing all files and directories, determining whether a file exists, and getting information about a file.
  • The Path module provides methods for printing the current working directory, changing the current directory, listing all files and directories, determining whether a file exists, and getting information about a file.
  • The shutil module provides methods for creating a new file or directory, renaming a file or directory, removing a file or directory, moving or copying a file, and zipping and unzipping files.
  • Python developers can automate tasks, such as removing out-of-date log files from a website, using these operations.
  • These operations can eliminate the need for manual file management.

File and Directory Management in Python: 10 Essential Operations

Python developers don’t need to open their file explorers

Photo by Compare Fibre on Unsplash

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')
False

Besides 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()
True

5. 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:

In addition, if we need to copy a file, the method is similar:

10. Zip and Unzip Files

If you are a Python expert, no zip software needs to be installed on your computer. Cause a few lines of Python code is enough to deal with the zip and unzip stuff of files.

The above code shows how to zip a file with the built-in ZipFile module. For unzipping a file, the usage is similar:

Key Takeaways

If you would like to automate some boring stuff about system management, Python is your best friend. Cause there are easy-to-use built-in modules, such as os, Path and shutil, prepared for you already.

We aced the file management skills now, but how to read or write the data inside a file? This is the technique about file I/O. Feel free to check out another informative article about it:

Thanks for reading. If you like it, please follow me and become a Medium member to enjoy more great articles about programming and technologies!

Programming
Python
Technology
Data Science
Software Development
Recommended from ReadMedium