OS Module

Import OS Module

First, we need to import the OS Module in our project. So for using the OS Module, we need to add using the โ€œimportโ€ statement. Along with that, we are gonna import a time module to make our program wait for some time using the sleep method.

import os
import time

Create a Function for Create Folders

As we know that for creating the function in Python we have to use โ€œdefโ€ keyword to define a function.

So letโ€™s create a new function โ€œcreateFolders(address)โ€ which accepts one argument as the address where we need to create folders.

So for this example letโ€™s create 10 directories or folders using OS built-in Module.

def createFolders(HOME_FOLDER):
    for i in range(10):
            os.mkdir(HOME_FOLDER + str(i) + '-Dir')

Create a Function for Create Files

Since we have declared a function which creates 10 folders at address which we passed as an argument.

Now letโ€™s create a new function for creating files named it as โ€œcreateFiles(arg)โ€ which accepts one argument as the address where we need to create files.

So for this example letโ€™s create 10 files using OS Module.

def createFiles(HOME_FOLDER):
    for i in range(10):
        f = open(HOME_FOLDER + str(i) + '-File.txt', 'w')
        f.close()

Create a Function for Rename Files

Now we have already defined the function for creating folders and files, so now letโ€™s create a new function for rename the files and folders.

Also Read => Flask Python Basic Application Tutorial [ Part-1 ]

As we already created the folders with โ€œ-Dirโ€ at last so now what we want is to replace this โ€œDirโ€ with Folder.

def renameFiles(HOME_FOLDER):
    os.chdir(HOME_FOLDER)
    for i in os.listdir():
        fileName, fileExt = os.path.splitext(i)
        print(fileName, fileExt)
        os.rename(i, i.replace('Dir', 'Folder'))

Create Main Function Code

Now we have created all the basic functions of the built-in Module. Now what we want is to create a main function to call them to show their functionality.

And also we are gonna use the time module which has sleep(seconds) in this main function to make a pause while creating and renaming files or folders.

if __name__ == '__main__':
    HOME_FOLDER = 'C:/CodezUp/Python/Scripts/Demo/'
    createFolders(HOME_FOLDER)
    createFiles(HOME_FOLDER)
    time.sleep(10)
    renameFiles(HOME_FOLDER)

Output

Source Code :

import os
import time

def createFolders(HOME_FOLDER):
    for i in range(10):
            os.mkdir(HOME_FOLDER + str(i) + '-Dir')

def createFiles(HOME_FOLDER):
    for i in range(10):
        f = open(HOME_FOLDER + str(i) + '-File.txt', 'w')
        f.close()

def renameFiles(HOME_FOLDER):
    os.chdir(HOME_FOLDER)
    for i in os.listdir():
        fileName, fileExt = os.path.splitext(i)
        print(fileName, fileExt)
        os.rename(i, i.replace('Dir', 'Folder'))

if __name__ == '__main__':
    HOME_FOLDER = 'C:/CodezUp/Python/Scripts/Demo/'
    createFolders(HOME_FOLDER)
    createFiles(HOME_FOLDER)
    time.sleep(10)
    renameFiles(HOME_FOLDER)

Last updated