The is a python module that provides the interface for interacting with the underlying that is running. OS module operating system Python This module provides a portable way of using dependent functionality. operating system comes by default on y modules therefore you don’t need to install anything to start using it OS module Python’s standard utilit OS module provides a ton of of which you use in variety of situation in with the ranging from s to and hem. methods interacting operating system creating new file & folder renaming deleting t You can view all of them by using as shown below dir() >>> os >>>dir(os) , , , , , , , , , , , , , , , , , , , , , , ............................and so on........................... import 'chown' 'chroot' 'close' 'closerange' 'confstr' 'confstr_names' 'cpu_count' 'ctermid' 'curdir' 'defpath' 'device_encoding' 'posix_fallocate' 'pread' 'putenv' 'pwrite' 'read' 'readlink' 'readv' 'remove' 'removedirs' 'removexattr' 'rename' 'renames' On this tutorial We will just check some of methods on OS Module os.getcwd ( ) This method is used to get the of the current working . OS path directory >>> os >>>os.getcwd() #getting path current directory import of '/home/kalebu' os.chdir ( ) This method is used to the current directory into a new directory, it receives one parameter which is the of the new directory. OS change path >>> os >>>os.getcwd() #getting path before changing dir >>>os.chdir( ) >>>os.getcwd() #getting path after changing dir import '/home/kalebu' 'Desktop' '/home/kalebu/Desktop' os.chdir ( ) This method is used to the current directory into a new directory, it receives one parameter which is the of the new directory. OS change path >>> os >>>os.getcwd() #getting path before changing dir >>>os.chdir( ) >>>os.getcwd() #getting path after changing dir import '/home/kalebu' 'Desktop' '/home/kalebu/Desktop' ) os.listdir ( This method returns all and on the current directory. OS files sub-directory >>> os >>>all_dirs = os.listdir() >>>print(all_dirs) [ , , ] import 'documentation' 'app.py' 'os methods .png' Also, you can a parameter of a specific directory to view its file and sub-directory pass >>> os >>> doc_dirs = os.listdir( ) >>> print(doc_dirs) [ , , ] import 'documentation' 'learn.py' 'requirements.txt' 'you.py' os.mkdir ( ) This method is used to create a new directory of the current path, for instance, let’s create a named OS directory Products. >>> os >>>os.listdir() #before any change [ , , ] >>>os.mkdir( ) # os.mkdir(directory_name) >>>os.listdir() #after making a dir [ , , , ] import 'documentation' 'app.py' 'os methods .png' 'Products' new 'documentation' 'Products' 'app.py' 'os methods .png' os.rename ( ) This Module is used to rename a or , it receives two-argument, an and a of file or directory. For instance, let’s rename to OS file folder old name new name app.py os.py >>> os >>>os.listdir() [ , , , ] >>> os.rename( , ) # os.rename( , ) >>> os.listdir() [ , , , ] import 'documentation' 'Products' 'app.py' 'os methods .png' 'app.py' 'os.py' 'oldname' 'newname' 'documentation' 'Products' 'os.py' 'os methods .png' os.rmdir ( ) This method is used to a directory it receives an argument of the of the directory you want to remove. will only work for empty dirs OS remove name os.rmdir() for instance, Let’s remove the directory using . Products rmdir() >>> os >>>os.listdir() [ , , , ] >>> os.rmdir( ) >>> os.listdir() [ , , ] import 'documentation' 'Products' 'app.py' 'os methods .png' 'Products' 'documentation' 'os.py' 'os methods .png' os.remove ( ) This OS Method is used to remove or delete a file path . For instance let’s remove y file os.p >>> os >>>os.listdir() [ , , ] >>> os.rmdir( ) >>> os.listdir() [ , ] import 'documentation' 'os.py' 'os methods .png' 'os methods .png' 'documentation' 'os.py' os.sytem ( ) This Method is used to run a shell command on the python program as if you were in shell. For instance, let’s run a command using the system method. OS tree >>> os >>> os.system( ) . ├── documentation │ ├── learn.py │ ├── requirements.txt │ └── you.py └── os.py directory, files import 'tree' 1 4 0 os.uname ( ) This Method returns information identifying the current operating system. OS >>> os >>> os.uname() posix.uname_result(sysname= , nodename= , release= , version= , machine= ) import 'Linux' 'kalebu-PC' '4.15.0-30deepin-generic' '#31 SMP Fri Nov 30 04:29:02 UTC 2018' 'x86_64' [] os.environ is not a method in the module rather than it’s a process parameter through which we can access the environment variables of the system. environ OS For instance Let’s access environment variable HOME >>> os >>> os.environ[ ] import 'HOME' '/home/kalebu' We also set our own as shown below environment variable >>> os >>> os.environ[ ] = #setting our own variable >>> os.environ[ ] #accesing it import 'my_secret_key' "Life sucks" 'my_secret_key' 'Life sucks' os.getuid ( ) This Module returns the current process’s user id. >>> os >>> os.getuid() import 1000 os.getpid ( ) Returns the real process ID of the current process. >>> os >>> os.getpid() import 19521 Summary In this Tutorial we learned about and how can you use it in a variety of operating system related tasks using its methods. OS Module Hope you find it interesting, now don’t be shy share it with your fellow peer on Twitter( ) and other dev communities. Tweet now In case of any suggestion, comment, or any difficulty drop it in the comment box below and I will get back to you ASAP. The original article can be found on kalebujordan.com