How to Manage Files in Python?

By | September 26, 2021
How to Manage Files in Python?

File Management is one of the features of the Operating System, and in Python, we get a dedicated os module or API to interact with System’s Operating System. When we work on a big Python Project there we might need to manage our file using the code itself. In that case, we can use the Python os module for file management, without worrying about the Operating System on which the code gonna run.

In this Python tutorial, I will walk you through the Python os module and how to use it to manage or handle files in Python.

Vamware

By the end of this tutorial, you will have a solid Idea on

  • How to print the current working directory in Python?
  • How to create New Directory in Python?
  • How to Change Directories in Python?
  • How to Create Nested Directories in Python?
  • How to Create Files in Python?
  • How to Rename Files in Python?
  • How to Move Files in Python?
  • How to list Files and Directories in Python?
  • How to Delete files in Python?
  • How to Retrieve Information about the file in Python?

How Print Current Working Directory in Python?

The current working directory is the location in your File system where you are currently present or where your Python script code is located.

To print the current directory in Python we will use the os module getcwd() function.

import os

#my current directory
my_directory = os.getcwd()

print("The Current Directory is:", my_directory)

Output

The Current Directory is: C:\Users\tsmehra\Desktop\code\tutorial

From the output, you can see that my Current Directory is C:\Users\tsmehra\Desktop\code\tutorial , this specifies my above Python program location.

How to Create Directory in Python?

The directory is also known as folder, and to create a new directory in Python we can use the os module mkdir(directory_name) function.

The mkdir() function will throw an error if the new directory name is similar to an existing directory name. In that case, we can use the Python exception handling try block to handle the error and throw a convenient error message.

import os

dir_name ="New Folder"

try:
    #create new directory
    os.mkdir(dir_name)
    print(f"New Directory {dir_name} has been Created")

except:
    print(f"!!!!!Error Directory Name {dir_name} Already Exist" )

Output

New Directory New Folder has been Created

How to Change Directories in Python?

In many cases, you wish to change the directory for writing files in another directory. However, that can be performed passing the absolute or relative path but in case of making a program run in the different operating systems, you need to use the change directory using os module.

To change the directory using os module we can use the chdir(dir_name) function.

import os

print("Your Python Program Directory is, ", os.getcwd())

#change working directory
os.chdir("New Folder")

print("Now, Your current directory is:", os.getcwd())

Output

Your Python Program Directory is, C:\Users\tsmehra\Desktop\code\tutorial
Now, Your current directory is: C:\Users\tsmehra\Desktop\code\tutorial\New Folder

How to create Nested Directories in Python?

In nested directories, we create directories inside a directory.

Although we can create a for loop and inside it we can keep changing the directory with chdir() and make new directories with mkdir(). But os module provides a makedirs() function that can create nested directories using Python.

import os

#create nested directories
os.makedirs("Level1/Level2/Level3/Level4")

print("Nested Directories have been Created")

Output

Nested Directories have been Created

How to Create New Files in Python?

We can use the Python I/O file handing to create, write, and read files.  Let’s create a new file by name new_file.txt inside the newly created New Folder directory.

import os

#change Current working directory
os.chdir("New Folder")

filename = "new_file.txt"

#create new file
with open("new_file.txt", 'w') as file:
    file.write("Some Random Data")
    print(f"New File {filename} has been created")

Output

New File new_file.txt has been created

How to Rename Files in Python?

To rename a file in Python we use the os module rename("file_name", "new_name") function.

Now let’s change the new_file.txt filename to renamed.txt that we created in the above example.

import os

#change Current working directory
os.chdir("New Folder")

try:
    new_name = "renamed.txt"
    os.rename("new_file.txt", new_name)  #rename
    print("File Name Has been Changed")
except:
    print(f"File With  {new_name} already exist ")
Output
File Name Has been Changed

How to move files in Python?

Let’s say you want to move the complete file from one location to another, in that case, you can use the Python os module replace() method to move files.

Let’s move the renamed.txt file from New Folderto thetutorial folder.

import os

#change Current working directory
os.chdir("New Folder")

try:
    #../ to go back in directory
    os.replace("renamed.txt", "../renamed.txt")
except:
    print("Could not move File already exist ")

In the replace() method I have used the ../renamed.txt string as a destination location. When you use the replace method you need to use the file name too for the destination. The ../ specify to go back one directory level.

to know how to copy files and directories in Python using shutil click here.

How to list all the Files and Directories in Python?

If you want to list all the directories and files present in a directory or in your current working directory, then you can use the os module listdir()function.

import os

print("All Directories and Files Present in ", os.getcwd(), "Directory are")

print(os.listdir())

Output

All Directories and Files Present in C:\Users\tsmehra\Desktop\code\tutorial Directory are
['Level1', 'New Folder', 'PythonFileManagement.py', 'renamed.txt']

The os.listdir() method will only list out the top-level directories and files present in the directory. If you want to list all the nested directories and files then you need to use the Python walk() function.

import os

print("All Directories and Files Present in ", os.getcwd(), "Directory are")

#walk through current directory "."
for _, dir_names, files in os.walk("."):

    #list directories
    for directory in dir_names:
        #join full path
        print("Directory:", os.path.join(os.getcwd(), directory))

    #list files
    for file in files:
        print("File:", os.path.join(os.getcwd(), file))

import os

print("All Directories and Files Present in ", os.getcwd(), "Directory are")

#walk through current directory "."
for _, dir_names, files in os.walk("."):

    #list directories
    for directory in dir_names:
        #join full path
        print("Directory:", os.path.join(os.getcwd(), directory))

    #list files
    for file in files:
        print("File:", os.path.join(os.getcwd(), file))

Output

All Directories and Files Present in C:\Users\tsmehra\Desktop\code\tutorial Directory are
Directory: C:\Users\tsmehra\Desktop\code\tutorial\Level1
Directory: C:\Users\tsmehra\Desktop\code\tutorial\New Folder
File: C:\Users\tsmehra\Desktop\code\tutorial\PythonFileManagement.py
File: C:\Users\tsmehra\Desktop\code\tutorial\renamed.txt
Directory: C:\Users\tsmehra\Desktop\code\tutorial\Level2
Directory: C:\Users\tsmehra\Desktop\code\tutorial\Level3
Directory: C:\Users\tsmehra\Desktop\code\tutorial\Level4

How to delete files in Python?

To delete a file we can use the os module remove() function.

Let’s delete the file "renamed.txt" that we moved in the above example.

import os

try:
    filename = "renamed.txt"
    os.remove(filename)
    print("File has been deleted")
except:
    print("Error! No Such File")

Output

File has been deleted

How to delete Directories in Python?

With os module rmdir() function we can delete only a single empty directory.

import os

#remove only empty directory
os.rmdir("New Folder")

If you want to delete nested empty directories, you can use the os module removedirs() function.

import os

#remove nested empty directories
os.removedirs('Level1/Level2/Level3/Level4')

The os.removedirs()will only remove all the nested directories if they all are empty. It will start deleting the directory from the bottom “Level4” in our case. And recursively deleting the directories to Level1.

Unfortunately, os module does not have a function that can delete directories containing files and other subdirectories.

If you want to delete a non-empty directory in Python, you can use the rmtree()function from shutil.

import shutil

#remove directory
shutil.rmtree("Level1")

How to Retrieve Information about Files in Python?

Let’s say you want to get metadata or information about the file, for that you can use the os module stat() function.

The stat()function returns a stats object of information about the file.

let’s first create a file by name new.txt and retrieve its information.

import os

#create new file
with open("new.txt", "w") as file:
    file.write("Some Random Data")

#print file information
print(os.stat("new.txt"))

Output

os.stat_result(st_mode=33206, 
              st_ino=12666373952022892, 
              st_dev=2689367036, 
              st_nlink=1, 
              st_uid=0, 
              st_gid=0,
              st_size=16, 
              st_atime=1612701969, 
              st_mtime=1612701969, 
              st_ctime=1612701955)

In the above output

st_mode represent file mode bits.

st_ino represent the inode number (Unix) or file index (Windows)

st_dev  represent identifier of the device.

st_nlink represent the number of hard links.

st_uid  represent the file owner’s user identifier.

st_gid represent the file owner’s group identifier.

st_size represent the size of the file(bytes).

st_atime represent the most recent access time in Unix timestamp (seconds).

st_mtime represent the most recent content modifier in Unix timestamp (seconds)

st_ctime represent the creation time in Unix timestamp (seconds)

Conclusion

In this tutorial, we learned how to use the Python os module to manage files and directories in Python. The os module is the standard Python module to interact with the system’s operating system.

To know more about the os module and its methods, please go through its official documentation provided by Python itself.

People are also reading: 

Leave a Reply

Your email address will not be published. Required fields are marked *