English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about file and directory management in Python, that is, creating a directory, renaming it, listing all directories, and using them.
If your Python program hasto handlea lotFilesIf so, you can organize the code across different directories to make things easier to manage.
A directory or folder is a collection of files and subdirectories. Python has os Modulewhich provides us with many useful methods for using directories (and files).
We can use the getcwd() method to get the current working directory.
This method returns the current working directory as a string. We can also use the getcwdb() method to obtain it as a byte object.
>>> import os >>> os.getcwd() 'C:\\Program Files\\PyScripter' >>> os.getcwdb() b'C:\\Program Files\\PyScripter'
Extra backslashes represent escape sequences. The print() function will display them correctly.
>>> print(os.getcwd()) C:\Program Files\PyScripter
We can use the chdir() method to change the current working directory.
The new path we want to change must be provided to this method as a string. We can use a forward slash (/)or backslash (\) to separate paths.
It is safer to use escape sequences when using backslashes.
>>> os.chdir('C:\\Python33') >>> print(os.getcwd()) C:\Python33
The listdir() method can be used to know all the files and subdirectories within the directory.
This method takes a path and returns a list of subdirectories and files within that path. If no path is specified, it returns the list from the current working directory.
>>> 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'
We can use the mkdir() method to create a new directory.
This method takes the path of the new directory. If the complete path is not specified, a new directory will be created in the current working directory.
>>> os.mkdir('test') >>> os.listdir() ['test']
The rename() method can rename directories or files.
The first parameter is the old name, and the new name must be the second parameter.
>>> os.listdir() ['test'] >>> os.rename('test','new_one') >>> os.listdir() ['new_one']
The remove() method can be used to delete (delete) files.
Similarly, the rmdir() method will delete an empty directory.
>>> os.listdir() ['new_one', 'old.txt'] >>> os.remove('old.txt') >>> os.listdir() ['new_one'] >>> os.rmdir('new_one') >>> os.listdir() []
However, please note that the rmdir() method can only delete empty directories.
To delete a non-empty directory, we can use the rmtree() method from the shutil module.
>>> 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() []