All the IPython Notebooks in Python Files lecture series by Dr. Milaan Parmar are available @ GitHub
In this class, you'll learn about file and directory management in Python, i.e. creating a directory, renaming it, listing all directories, and working with them.
If there are a large number of files to handle in our Python program, we can arrange our code within different directories to make things more manageable.
A directory or folder is a collection of files and subdirectories. Python has the os
module that provides us with many useful methods to work with directories (and files as well).
getcwd()
-¶We can get the present working directory using the getcwd()
method of the os module.
This method returns the current working directory in the form of a string. We can also use the getcwd()
method to get it as bytes object.
import os
print(os.getcwd())
C:\Users\Deepak\01_Learn_Python4Data\05_Python_Files
import os
os.getcwdb()
b'C:\\Users\\Deepak\\01_Learn_Python4Data\\05_Python_Files'
The extra backslash implies an escape sequence. The print()
function will render this properly.
chdir()
-¶We can change the current working directory by using the chdir()
method.
The new path that we want to change into must be supplied as a string to this method. We can use both the forward-slash /
or the backward-slash \
to separate the path elements.
It is safer to use an escape sequence when using the backward slash.
Syntax:
os.chdir("newdir")
Remember to:
import os
# Changing a directory to "C:\Users\Deepak\OneDrive\Desktop\xyz"
os.chdir(r"C:\Users\Deepak\OneDrive\Desktop\xyz")
print("Directory changed")
print(os.getcwd())
Directory changed C:\Users\Deepak\OneDrive\Desktop\xyz
import os
print(os.getcwd())
C:\Users\Deepak\OneDrive\Desktop\xyz
import os
# Changing a directory back to original directory "C:\Users\Deepak\01_Learn_Python4Data\05_Python_Files"
os.chdir(r"C:\Users\Deepak\01_Learn_Python4Data\05_Python_Files")
print("Directory changed")
Directory changed
import os
print(os.getcwd())
C:\Users\Deepak\01_Learn_Python4Data\05_Python_Files
listdir()
-¶The listdir()
method displays all files and sub-directories inside a directory.
This method takes in a path and returns a list of subdirectories and files in that path. If no path is specified, it returns the list of subdirectories and files from the current working directory.
print(os.getcwd())
os.listdir()
C:\Users\Deepak\01_Learn_Python4Data\05_Python_Files
['.ipynb_checkpoints', '001_Python_File_Input_Output.ipynb', '002_Python_File_Directory.ipynb', '003_Python_File_Exception.ipynb', '004_Python_Exceptions_Handling.ipynb', '005_Python_User_defined_Exceptions.ipynb', 'data.txt', 'data_1.txt', 'img', 'logo.png', 'logo1.png', 'test.txt', 'testfile', 'test_1.txt', 'test_2.txt']
os.listdir('C:\\')
['$Recycle.Bin', '$WinREAgent', '$WINRE_BACKUP_PARTITION.MARKER', 'bootTel.dat', 'Documents and Settings', 'DumpStack.log', 'DumpStack.log.tmp', 'hiberfil.sys', 'ImDisk', 'OneDriveTemp', 'pagefile.sys', 'PerfLogs', 'Program Files', 'Program Files (x86)', 'ProgramData', 'Python99', 'Recovery', 'swapfile.sys', 'System Volume Information', 'Users', 'Windows']
mkdir()
-¶You can use the mkdir()
method of the os module to create directories in the current directory. You need to supply an argument to this method, which contains the name of the directory to be created.
This method takes in the path of the new directory. If the full path is not specified, the new directory is created in the current working directory.
Syntax:
os.mkdir("dir_name")
import os
os.mkdir('python_study')
print("Directory created")
os.listdir()
Directory created
['.ipynb_checkpoints', '001_Python_File_Input_Output.ipynb', '002_Python_File_Directory.ipynb', '003_Python_File_Exception.ipynb', '004_Python_Exceptions_Handling.ipynb', '005_Python_User_defined_Exceptions.ipynb', 'data.txt', 'data_1.txt', 'img', 'logo.png', 'logo1.png', 'python_study', 'test.txt', 'testfile', 'test_1.txt', 'test_2.txt']
rename()
-¶The rename()
method can rename a directory or a file.
Syntax:
os.rename(current_file_name, new_file_name)
os.listdir()
os.rename('python_study','python_learning')
print("Directory renamed")
os.listdir()
Directory renamed
['.ipynb_checkpoints', '001_Python_File_Input_Output.ipynb', '002_Python_File_Directory.ipynb', '003_Python_File_Exception.ipynb', '004_Python_Exceptions_Handling.ipynb', '005_Python_User_defined_Exceptions.ipynb', 'data.txt', 'data_1.txt', 'img', 'logo.png', 'logo1.png', 'python_learning', 'test.txt', 'testfile', 'test_1.txt', 'test_2.txt']
import os
os.rename('data_1.txt','my_data.txt')
print("file renamed")
file renamed
remove()
and rmdir()
-¶A file can be removed (deleted) using the remove()
method.
Similarly, the rmdir()
method removes an empty directory. Before removing a directory, all the contents in it should be removed.
import os
# This would remove "C:\Users\Deepak\OneDrive\Desktop\xyz" directory.
os.rmdir(r"C:\Users\Deepak\OneDrive\Desktop\xyz")
print("Directory deleted")
os.listdir()
Directory deleted
['.ipynb_checkpoints', '001_Python_File_Input_Output.ipynb', '002_Python_File_Directory.ipynb', '003_Python_File_Exception.ipynb', '004_Python_Exceptions_Handling.ipynb', '005_Python_User_defined_Exceptions.ipynb', 'data.txt', 'img', 'logo.png', 'logo1.png', 'my_data.txt', 'python_learning', 'test.txt', 'testfile', 'test_1.txt', 'test_2.txt']
import os
os.remove('my_data.txt')
print("file deleted")
file deleted
Note: The
rmdir()
method can only remove empty directories.
In order to remove a non-empty directory, we can use the rmtree()
method inside the shutil
module.
import shutil
shutil.rmtree('python_learning')
os.listdir()
['.ipynb_checkpoints', '001_Python_File_Input_Output.ipynb', '002_Python_File_Directory.ipynb', '003_Python_File_Exception.ipynb', '004_Python_Exceptions_Handling.ipynb', '005_Python_User_defined_Exceptions.ipynb', 'data.txt', 'img', 'logo.png', 'logo1.png', 'test.txt', 'testfile', 'test_1.txt', 'test_2.txt']