How To Use Python OS Module Tutorial

Python os module is a module in the python standard library for accessing operating system related functions. The os module provide a portable method of using operating system functions. Use the interface provided in python os module, cross platform access can be achieved. However, not all interfaces in the os module are universal used on all platforms. Some interfaces are implemented on a specific platform, such as Linux-related file permission management and process management.

Below is the main functions of the python os module:

  1. System related.
  2. Directory and file operations.
  3. Command execution.
  4. Management of processes.

1. Import Python os Module.

Before use os module functions and variables, you should first import os module use import os command, otherwise it will throw NameError: name ‘os’ is not defined error when you use it.

>>> os.altsep
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined

2. System Related Functions And Variables.

The os module provide some operating system-related variables, which can provide support for cross-platform usage, making it easy to write code with high portability and good usability. Therefore, when it comes to operating system-related operations, please try to use the methods provided in os module instead of using the operating system platform-specific function, otherwise, once run the python code in a different os platform, it may cause error.

Below are os module’s functions and variables list.

  1. os.name : View the name of the current operating system. Under windows platform it return ‘nt’, linux platform return ‘posix’.
    >>> import os
    >>> os.name
    'nt'
  2. os.environ : Get system environment variables.
    >>> import os
    >>> os.environ
    environ({'ALLUSERSPROFILE': 'C:\\ProgramData', 'APPDATA': 'C:\\Users\\song zhao\\AppData\\Roaming', 'COMMONPROGRAMFILES': 'C:\\Program Files\\Common Files', 'COMMONPROGRAMFILES(X86)': 'C:\\Program Files (x86)\\Common Files', 'COMMONPROGRAMW6432': 'C:\\Program Files\\Common Files', 'COMPUTERNAME': 'DESKTOP-Q26A5F3', 'COMSPEC': 'C:\\Windows\\system32\\cmd.exe', 'CONDA_DEFAULT_ENV': 'env_jupyter_example', 'CONDA_EXE': 'C:\\Anaconda\\Scripts\\conda.exe', 'CONDA_PROMPT_MODIFIER': '(env_jupyter_example) ', 'CONDA_PYTHON_EXE': 'C:\\Anaconda\\python.exe', 'CONDA_ROOT': 'C:\\Anaconda', 'CONDA_SHLVL': '1', 'DRIVERDATA': 'C:\\Windows\\System32\\Drivers\\DriverData', 'FPS_BROWSER_APP_PROFILE_STRING': 'Internet Explorer', 'FPS_BROWSER_USER_PROFILE_STRING': 'Default', 'HOMEDRIVE': 'C:', 'HOMEPATH': '\\Users\\song zhao', 'LOCALAPPDATA': 'C:\\Users\\song zhao\\AppData\\Local', 'LOGONSERVER': '\\\\DESKTOP-Q26A5F3', 'NUMBER_OF_PROCESSORS': '2', 'ONEDRIVE': 'C:\\Users\\song zhao\\OneDrive', 'OS': 'Windows_NT', 'PATH': 'C:\\Anaconda\\envs\\env_jupyter_example;C:\\Anaconda\\envs\\env_jupyter_example\\Library\\mingw-w64\\bin;C:\\Anaconda\\envs\\env_jupyter_example\\Library\\usr\\bin;C:\\Anaconda\\envs\\env_jupyter_example\\Library\\bin;C:\\Anaconda\\envs\\env_jupyter_example\\Scripts;C:\\Anaconda\\envs\\env_jupyter_example\\bin;C:\\Anaconda\\condabin;C:\\Anaconda;C:\\Anaconda\\Library\\mingw-w64\\bin;C:\\Anaconda\\Library\\usr\\bin;C:\\Anaconda\\Library\\bin;C:\\Anaconda\\Scripts;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0;C:\\Windows\\System32\\OpenSSH;C:\\Python38\\Scripts;C:\\Python38;C:\\Users\\song zhao\\AppData\\Local\\Microsoft\\WindowsApps;.', 'PATHEXT': '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC', 'PROCESSOR_ARCHITECTURE': 'AMD64', 'PROCESSOR_IDENTIFIER': 'Intel64 Family 6 Model 58 Stepping 9, GenuineIntel', 'PROCESSOR_LEVEL': '6', 'PROCESSOR_REVISION': '3a09', 'PROGRAMDATA': 'C:\\ProgramData', 'PROGRAMFILES': 'C:\\Program Files', 'PROGRAMFILES(X86)': 'C:\\Program Files (x86)', 'PROGRAMW6432': 'C:\\Program Files', 'PROMPT': '(env_jupyter_example) $P$G', 'PSMODULEPATH': 'C:\\Program Files\\WindowsPowerShell\\Modules;C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\Modules', 'PUBLIC': 'C:\\Users\\Public', 'QT_API': 'pyqt5', 'SESSIONNAME': 'Console', 'SYSTEMDRIVE': 'C:', 'SYSTEMROOT': 'C:\\Windows', 'TEMP': 'C:\\Users\\SONGZH~1\\AppData\\Local\\Temp', 'TMP': 'C:\\Users\\SONGZH~1\\AppData\\Local\\Temp', 'USERDOMAIN': 'DESKTOP-Q26A5F3', 'USERDOMAIN_ROAMINGPROFILE': 'DESKTOP-Q26A5F3', 'USERNAME': 'song zhao', 'USERPROFILE': 'C:\\Users\\song zhao', 'WINDIR': 'C:\\Windows', 'CONDA_PREFIX': 'C:\\Anaconda\\envs\\env_jupyter_example'})
  3. os.sep : The path separator of the current platform. In windows, it is ‘\’, in POSIX system, it is ‘/’.
    >>> import os
    >>> os.sep
    '\\'
  4. os.altsep : Alternative path separator, it is ‘/’ in Windows.
    >>> import os
    >>> os.altsep
    '/'
  5. os.extsep : The symbol that separates the file name and file extension. Under Windows, it is ‘.’
    >>> import os
    >>> os.extsep
    '.'
  6. os.pathsep : The separator in the PATH environment variable, it is ‘:’ in POSIX systems and ‘;’ in Windows.
    >>> import os
    >>> os.pathsep
    ';'
  7. os.linesep : Line ending character. The ending character of the end of the line is different in different systems, such as ‘\r\n’ in Windows.
    >>> import os
    >>> os.linesep
    '\r\n'
  8. os.devnull : The path of the null device on different systems. It is ‘nul’ for Windows and ‘/dev/null’ for Linux.
    >>> import os
    >>> os.devnull
    'nul'
  9. os.defpath : When using the exec function family, if the PATH environment variable is not specified, the value in os.defpath will be found by default as the value of the child process PATH.
    >>> import os
    >>> os.defpath
    '.;C:\\bin'

3. File And Directory Operations.

  1. os.getcwd() : Get the current working directory, that is, the directory path where the current python script execute.
    >>> import os
    >>> os.getcwd()
    'C:\\Users\\song zhao'
  2. os.chdir(“dirname”) : Change the working directory of the current script, equivalent to cd command in shell.
    >>> import os
    >>> os.chdir("C:\WorkSpace")
    >>> os.getcwd()
    'C:\\WorkSpace'
  3. os.curdir : Return the current directory: (‘.’).
    >>> import os
    >>> os.curdir
    '.'
  4. os.pardir : Get the string name of the parent directory of the current directory: (‘..’).
    >>> import os
    >>> os.pardir
    '..'
  5. os.mkdir(‘dirname’) : Generate single-level directory.
    >>> import os
    >>> os.mkdir('c:/abc')
  6. os.rmdir(‘dirname’) : Delete a single-level empty directory, if the directory is not empty, it cannot be deleted and an error is reported.
    >>> import os
    # create a multiple level directory tree.
    >>> os.makedirs('C:/abc/def/hgk')
    
    # go to the hgk directory.
    >>> os.chdir('c:/abc/def/hgk')
    
    # remove directory c:/abc will throw error, because it is not am empty directory. 
    >>> os.rmdir('c:/abc')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    OSError: [WinError 145] The directory is not empty: 'c:/abc'
    
    # remove the ending empty directory failed because current directory is 'c:/abc/def/hgk'
    >>> os.rmdir('c:/abc/def/hgk')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'c:/abc/def/hgk'
    
    # change current working directory to 'c:/abc'
    >>> os.chdir('c:/abc')
    
    # remove sub directory of 'c:/abc' successfully.
    >>> os.rmdir('c:/abc/def/hgk')
    >>>
  7. os.makedirs(‘dir1/dir2’) : Can generate multiple recursive directories.
    >>> import os
    >>> os.makedirs("C:/a/b/c")
    >>> os.listdir("C:/a")
    ['b']
  8. os.removedirs(‘dirname’) : Recursively delete empty directories (be careful), if the ending directory is not empty, then the operation failed, and it will throw an OSError.
    # because there is another directory c in c:/a/b, then below operation will throw OSError which said the directory is not empty.
    >>> os.removedirs('c:/a/b')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\Anaconda\envs\env_jupyter_example\lib\os.py", line 241, in removedirs
        rmdir(name)
    OSError: [WinError 145] The directory is not empty: 'c:/a/b'
    >>>
    >>>
    # below operation will success, it will remove both a,b,c directory.
    >>> os.removedirs('c:/a/b/c')
  9. os.listdir(‘dirname’) : List all files and subdirectories in the specified directory, including hidden files.
    >>> import os
    >>> os.listdir('c:/abc')
    ['def']
  10. os.remove(‘filename’) : Delete a file.
    >>> import os
    
    # change current working directory to c:/abc.
    >>> os.chdir('c:/abc')
    
    # because def is a directory in this example, so the remove method can not remove it.
    >>> os.remove('./def')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    PermissionError: [WinError 5] Access is denied: './def'
    
    # remove file c:/abc/def/test.txt will success.
    >>> os.remove('./def/test.txt')
  11. os.rename(“old-name”,”new-name”) : Rename file/directory.
    # create an empty text file.
    >>> open('test.txt', 'a').close()
    >>>
    
    >>> import os
    
    # rename the file to a new name.
    >>> os.rename('test.txt', 'test-new.txt')
    >>> os.listdir('./')
    ['test-new.txt']
    
    # get current working directory.
    >>> os.getcwd()
    'c:\\abc\\def'
    
    # go to current directory's parent directory c:/abc.
    >>> os.chdir('../')
    >>> os.getcwd()
    'c:\\abc'
    
    # rename directory c:/abc/def to c:/abc/def-new
    >>> os.rename('./def','def-new')
    >>> os.listdir('./')
    ['def-new']
  12. os.stat(‘path/filename’) : Get file/directory information.
    >>> import os
    
    # get current working directory.
    >>> os.getcwd()
    'c:\\abc'
    
    # get a directory's status information.
    >>> os.stat('./def-new')
    os.stat_result(st_mode=16895, st_ino=844424930156964, st_dev=3874382982, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1596523570, st_mtime=1596523507, st_ctime=1596521788)
    >>>
    # get a file's status information.
    >>> os.stat('./def-new/test-new.txt')
    os.stat_result(st_mode=33206, st_ino=2533274790421024, st_dev=3874382982, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1596523468, st_mtime=1596523468, st_ctime=1596523468)
  13. os.path.abspath(path) : Return the normalized absolute path of path.
    >>> import os
    >>> os.path.abspath('./')
    'c:\\abc'
  14. os.path.split(path) : Split path into a tuple which contain directory and file name and return it.
    >>> import os
    
    # get current path's absolute path value.
    >>> os.path.abspath('./')
    'c:\\abc'
    >>>
    
    # split current working directory path.
    >>> os.path.split(os.path.abspath('./'))
    ('c:\\', 'abc')
    
    # split a text file's path.
    >>> os.path.split('c:/abc/def-new/test-new.txt')
    ('c:/abc/def-new', 'test-new.txt')
    >>>
  15. os.path.dirname(path) : Return the directory part of a path value. It is actually the first element of os.path.split(path).
    >>> import os
    >>> os.path.dirname('c:/abc/def-new/test-new.txt')
    'c:/abc/def-new'
  16. os.path.basename(path) : Return the file name at the end of the path. If path ends with / or \, then an empty string value ( ” ) will be returned.
    >>> import os
    
    >>> os.path.basename('c:/abc/def-new/test-new.txt')
    'test-new.txt'
    
    >>> os.path.basename('c:/abc/def-new/')
    ''
    
    >>> os.path.basename('c:/abc/def-new')
    'def-new'
  17. os.path.exists(path / file) :  If path/file exists, return True, if path/file does not exist, return False.
    >>> import os
    
    >>> os.path.exists('c:/abc/def-new')
    True
    >>> os.path.exists('c:/abc/def-new/test.txt')
    False
  18. os.path.isabs(path) : If path is an absolute path, return True.
    >>> import os
    
    >>> os.path.isabs('./')
    False
    
    >>> os.path.isabs('c:/')
    True
  19. os.path.isfile(path) : If path is an existing file, return True. Otherwise return False.
    >>> import os
    
    >>> os.path.isfile('c:/')
    False
    
    >>> os.path.isfile('c:/abc/def-new/test-new.txt')
    True
  20. os.path.isdir(path) : If path is an existing directory, it return True. Otherwise return False.
    >>> import os
    >>> os.path.isdir('c:/abc')
    True
    
    >>> os.path.isdir('c:/abc/def-new/test-new.txt')
    False
  21. os.path.join(path1[, path2[, …]]) : Combine multiple paths and return, the parameters before the first absolute path will be ignored.
    >>> import os
    
    # combine three path element.
    >>> os.path.join('c:\\','abc','def')
    'c:\\abc\\def'
    
    # return the first absolute path value.
    >>> os.path.join('c:\\','abc','c:/abc/def-new/test-new.txt')
    'c:/abc/def-new/test-new.txt'
    >>>
  22. os.path.getatime(path) : Return the last access time of the file or directory by path.
    >>> import os
    
    # get current working path.
    >>> curr_path = os.getcwd()
    >>> print(curr_path)
    c:\abc
    
    # get above path time.
    >>> os.path.getatime(curr_path)
    1596523579.3351827
  23. os.path.getmtime(path) : Return the last modification time of the file or directory by path.
    >>> import os
    
    >>> os.path.getmtime("c:/abc/def-new")
    1596523569.1780078
  24. os.path.getsize(filename) : Return the file size.
    >>> import os
    
    >>> os.path.getsize('c:/abc/def-new/test-new.txt')
    0