Home » Python

OS Module in Python with Examples

Python OS Module: Here, we are going to learn about the os module and some of their important functions with some examples in the Python programming language.
Submitted by Bipin Kumar, on December 10, 2019

Python os Module

The os module provides us with a lot of functions that used to interact with the operating system. There is no need to install the os module because it is an inbuilt module in Python. By using it, we can perform more tasks such as we can get the name of the operating system, navigate the file system, and many other operations are performed.

There are many directives in the os module. We can know the all directive by using the dir() function. Let's see it,

# importing the os module
import os 

# printing the directives
print('All directive of os module are:',dir(os))

Output

All directive of os module are: ['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 
'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 
'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 
'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 
'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_Environ', 
'__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', 
'__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', 
'_get_exports_list', '_putenv', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 
'altsep', 'chdir', 'chmod', 'close', 'closerange', 'cpu_count', 'curdir', 'defpath', 
'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'error', 'execl', 'execle', 
'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 
'fsdecode', 'fsencode', 'fspath', 'fstat', 'fsync', 'ftruncate', 'get_exec_path', 
'get_handle_inheritable', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 
'getenv', 'getlogin', 'getpid', 'getppid', 'isatty', 'kill', 'linesep', 'link', 
'listdir', 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 
'pathsep', 'pipe', 'popen', 'putenv', 'read', 'readlink', 'remove', 'removedirs', 
'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sep', 'set_handle_inheritable', 
'set_inheritable', 'spawnl', 'spawnle', 'spawnv', 'spawnve', 'st', 'startfile', 
'stat', 'stat_result', 'statvfs_result', 'strerror', 'supports_bytes_environ', 
'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 
'symlink', 'sys', 'system', 'terminal_size', 'times', 'times_result', 'truncate', 
'umask', 'uname_result', 'unlink', 'urandom', 'utime', 'waitpid', 'walk', 'write']

In this tutorial, we will cover some important functions of the os module.

Some important function of the os module

1) os.name

This function gives the name of the operating system that we are running in own system. The output of this function may vary from system to system because all are not using the same operating system.

# importing the os module
import os 

# getting & printing the name of the OS
print( 'Name of operating system: ',os.name)

Output

Name of operating system:  nt #windows nt is 32 bit operating system.

2) os.getcwd()

This function returns the name of the current working directive of the file that was used to execute the code.

# importing the os module
import os 

# getting & printing the current working directory
print( 'location of the file:',os.getcwd())

Output

location of the file: C:\Users\BIPIN KUMAR

3) os.chdir()

This function is used to change the path of the file that was used to execute the code. It takes the new path in the form of string as a parameter. Before going to change the directory, we make a new directory by using the mkdir() function.

# importing the os module
import os 

# creates a temporary directory
os.mkdir('d:\\tempdirectory') 
# getting & printing the current working directory
print( 'location of the file:',os.getcwd())

# changing the directory
os.chdir('E:\\tempdirectory') 
# getting & printing the current working directory
print( 'New location of the file:',os.getcwd())

Output

location of the file: C:\Users\BIPIN KUMAR
New location of the file: E:\tempdirectory


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.