Python Directory, File Management

    In this tutorial, we will discuss the python directory and file management. We will create a directory using python and learn how can we rename and delete those.

    Directory in Python

    Directory in simple words can be understood as a folder. When we work on a big project, we do not write code in a single file, we make multiple .py files and save those in multiple folders. So, these folders in which we save, our .py file or source file or script file, those folders could be known as python directory. When we deal with directories using python we use the os module.

    Current directory:

    To get the current directory folder on which your script is saved we use the getcwd() and getcwdb() methods. getcwd() return a string representation of directory whereas the getcwdb() methods return a byte object.

    Example:

    import os
    
    print("this file is stored at: ",os.getcwd())              #in string form
    print("this file is stored at: ",os.getcwdb())           #as a byte object  

    #Output

    this file is stored at:  C:\Users\techgeekbuzz\Documents\sublime
    this file is stored at:  b'C:\\Users\\techgeekbuzz\\Documents\\sublime'

    Behind the code

    In the second output, we can see there are double backslash \\ here the first backslash represents the path separator of the directory where the second backslash is just an escape sequence.

    Change the directory

    Python has a chdir() method which can change the current working directory. In chdir() method we need to pass a new directory for the file.

    Example:

    import os
    print("the directoy of the script before the chdir() method: ", os.getcwd())
    os.chdir("C:\\Users\\techgeekbuzz")       #use double backslash(\\) instead of single(\)
    print("-----------------")
    print("the directory after using the chdir() method:", os.getcwd())

    #Output

    the directory of the script before the chdir() method:  C:\Users\techgeekbuzz\Documents\sublime
    -----------------
    the directory after using the chdir() method: C:\Users\techgeekbuzz

    List Directories and files:

    With the help of listdir() method, we can list all the files and subdirectory of that directory in which our script is stored.

    Example:

    import os
    print(os.listdir())

    #output

    ['file.txt', 'python.py', '__pycache__']

    We can also pass the directory to the listdir() method to check what files that particular directory has.

    Example:

    import os
    print(os.listdir("C:\\"))   #all the files inside C drive

    #Output

    ['Apache24', 'Documents and Settings', 'DRIVERS', 'Intel', 'Program Files', 'Program Files (x86)', 'ProgramData', 'Recovery', 'Temp', 'Users', 'Windows']

    Make a New Directory or folder using Python

    With the help of mkdir() method, we can create a new directory . We have to pass the path of the new directory as an argument to mkdir() method.

    Syntax:

    os.mkdir('new_directory_name')

    Example:

    import os
    print("all file and subdirectory inside the current directory before using mkdir() method: ",os.listdir())
    os.mkdir('new_dir')
    print("all file and subdirectory inside the current directory after using mkdir() method: ",os.listdir())

    #Output:

    all file and subdirectory inside the current directory before using mkdir() method:  ['file.txt', ‘python.py', '__pycache__']
    all file and subdirectory inside the current directory after using mkdir() method:  ['file.txt', ‘python.py 'new_dir','__pycache__']

    Rename the Directory

    With the help of rename() method, we can rename the directory that we have just created. With rename() method we pass two arguments the first argument would be the name of that directory which we want to rename and the second argument would be the new name.

    Syntax:

    os.rename(‘dir_name_to _be_changed’ , ‘new_name for_the _directory’)

    Example:

    import os
    print("all file and subdirectory inside the current directory before using rename method: ",os.listdir())
    os.rename('new_dir','new_dir_2.0')
    print("all file and subdirectory inside the current directory after using rename method: ",os.listdir())

    #Output

    all file and subdirectory inside the current directory before using rename method:  ['file.txt', 'new_dir', 'python.py', '__pycache__']
    all file and subdirectory inside the current directory after using rename method:  ['file.txt', new_dir_2.0', 'python.pyl', '__pycache__']

    Delete Directory or file

    With the help of remove() and rmdir() methods, we can delete or remove the files and empty directory respectively.

    Example:

    import os
    os.remove('file.txt')
    os.rmdir('new_dir_2.0')
    print(os.listdir())

    #output

    ['python.py','__pycache__']