This tutorial will teach you how to manage files and directories in Python, including how to create a directory, rename it, list all directories, and operate with them.
If our Python software has a big number of files to handle, we can organize our code into multiple directories to make things easier to manage.
A directory, often known as a folder, is a grouping of files and subdirectories. The os module in Python provides numerous helpful methods for working with directories (and files as well).
Get Current Directory
The getcwd() method of the os module can be used to obtain the current working directory.
The current working directory is returned as a string by this procedure. We can also get it as a bytes object using the getcwdb() method.
>>> import os >>> os.getcwd() 'C:\\Program Files\\PyScripter' >>> os.getcwdb() b'C:\\Program Files\\PyScripter'
An escape sequence is implied by the extra backslash. This will be rendered correctly using the print() function.
>>> print(os.getcwd()) C:\Program Files\PyScripter
Using the chdir() technique, we may change the current working directory.
The new route that we wish to change to must be passed to this method as a string. To separate path items, we can use either the forward-slash / or the backward-slash.
When utilizing the backward slash, it is safer to use an escape sequence.
>>> os.chdir('C:\\Python33') >>> print(os.getcwd()) C:\Python33
List Directories and Files
The listdir() method can be used to obtain all files and subdirectories within a directory.
This method accepts a path and returns a list of its subdirectories and files. It returns the list of subdirectories and files from the current working directory if no path is supplied.
>>> print(os.getcwd()) C:\Python33 >>> os.listdir() ['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'README.txt', 'Scripts', 'tcl', 'Tools'] >>> os.listdir('G:\\') ['$RECYCLE.BIN', 'Movies', 'Music', 'Photos', 'Series', 'System Volume Information']
Making a New Directory
The mkdir() method can be used to create a new directory.
The path to the new directory is sent to this procedure. The new directory is created in the current working directory if the full path is not supplied.
>>> os.mkdir('test') >>> os.listdir() ['test']
Renaming a Directory or a File
A directory or a file can be renamed using the rename() method.
The rename() method accepts two basic arguments to rename any directory or file: the old name as the first argument and the new name as the second argument.
>>> os.listdir() ['test'] >>> os.rename('test','new_one') >>> os.listdir() ['new_one']
Removing Directory or File
The remove() method can be used to delete (delete) a file.
The rmdir() function, on the other hand, removes an empty directory.
>>> os.listdir() ['new_one', 'old.txt'] >>> os.remove('old.txt') >>> os.listdir() ['new_one'] >>> os.rmdir('new_one') >>> os.listdir() 
Note: Only empty directories can be removed with the rmdir() technique.
The rmtree() method in the shutil module can be used to remove a non-empty directory.
>>> os.listdir() ['test'] >>> os.rmdir('test') Traceback (most recent call last): ... OSError: [WinError 145] The directory is not empty: 'test' >>> import shutil >>> shutil.rmtree('test') >>> os.listdir()