What is the purpose of OS module

 

  • OS enables the python to interact with multiple platforms
  •  

    Create a directory:

    Directory denotes about folder

     

    What is the difference between mkdir, makedirs:

    If you want to create a sub folder(folder inside a folder) by using mkdir., we have to do in two steps., first create external next internal. Coming to 'makedirs' it enable the python to create all the folders at once in hierarchy

     

    glob:

    This function provides recursive search in subdirectories

     

    Create directory

     

     

    Create directory - on hierarchy

     

     

    Print all files in a folder

     

    #Retrieve total files in current folder
    import os
    AllFilesInAFolder = os.listdir(os.curdir)
    for filename in AllFilesInAFolder:
    print(filename)

    Print python files in a folder

     

    Print python files in current directory

    # list python files in a folder
    import glob
    filenames = glob.glob('*.py')
    print(filenames)

     

    Change the name of directory\folder

     

    import os
    # create a directory
    os.mkdir('tricks')
    print('Directory Created')
    # Change the directory name
    os.rename('tricks', '12345')
    print('Directory renamed')


     

    Get current working directory path

     


     

    sys.path

     

    import sys
    sys.path


     

    Find the Path of file

     

    import os
    pathname = os.path.abspath(os.path.dirname(__file__))
    print(pathname)