- os module provide a way to handle Operating System Related Functionality
- The os module is a part of the standard library.
- The primary use of the os module create a folder, remove a folder, rename, access files with path and name.
- The OS module in Python provides functions for interacting with the operating system.
import os module
import os
List of Avialable Functions
- To get the list of avialable function use dir().
Example:
#import os module
import os
#Get Help of os module
print(help(os))
#Get avialable Function list
print(dir(os))
Important Functions related to directory
- Returns the Operating system name such as,nt,posix,java,os2,ce
Example:
import os
#Get Name
name=os.name
print(name)
Output:
nt
- Return current working directory name.
- The path of the file used to execute the code.
Example:
#import required module
import os
#Get Current Working directory
direct=os.getcwd()
print(direct)
Output:
C:\\python3
- Chidr() used to change the working directory of the current path
syntax:
os.chdir(path)
Example:
#import module
import os
#get current working path
print(os.getcwd())
#Result:C:\\python3
#Change directory path to C:\Users\ravi\Desktop\demo
path=r'C:\Users\ravi\Desktop\demo'
os.chdir(path)
#Now get the new path
print(os.getcwd())
#Result:C:\\Users\\ravi\\Desktop\\demo
Output:
C:\\python3
C:\\Users\\ravi\\Desktop\\demo
- mkdir() create a specified folder.
- If the specified folder already exists it will Generate an Error
Cannot create a file when that file already exists: 'FolderName'
Syntax:
os.mkdir(path)
Example:
import os
#This will create a folder at os.getcwd() location
os.mkdir('testFolder')
- makedirs() is the super version of mkdir()
- It is used to create a leaf directory.
- If the folder already exists it will give an error.
- If parent directory is not available makedirs() function creates a parent directory.
Syntax:
os.makedirs('directory1/Directory2/dirname')
Example:
#import required module
import os
#make tree-like folder directory.
os.makedirs('test/testFolders')
'''
this will create a testFolders folder in the test directory
if test directory is not present makedirs() will create it
'''
- rename() used to rename the directory name. It will Return error if specified directory is not avialable
Syntax:
os.name(oldFolderName,newFolderName)
Example:
#import module
import os
#Create a folder with name testFolder
os.mkdir('testFolder')
#Rename That testFolder with newFolderName
os.rename('testFolder','newFolderName')
- rmdir() function used to remove the empty folder only if folder contains files it will generate error.
- if folder is not avialable it will give Error.
Syntax:
os.rmdir('folderName')
Example:
#import module
import os
#Remove testFolder
os.rmdir('testFolder')
- removedirs() is the super version of rmdir will remove empty directory .
- remove a leaf directory and all empty intermediate ones.
Syntax:
os.removedirs(path)
Example:
import os
os.removedirs('tst/testFolder')
- chmod() function used to change the mod of director and files
Syntax:
chmod(path,mode)
path:-This is the path for which mode would be set.
chmod:-This may take one of the following mentioned values
stat.S_ISUID − Set user ID on execution.
stat.S_ISGID − Set group ID on execution.
stat.S_ENFMT − Record locking enforced.
stat.S_ISVTX − Save text image after execution.
stat.S_IREAD − Read by owner.
stat.S_IWRITE − Write by owner.
stat.S_IEXEC − Execute by owner.
stat.S_IRWXU − Read, write and execute by owner.
stat.S_IRUSR − Read by owner.
stat.S_IWUSR − Write by owner.
stat.S_IXUSR − Execute by owner.
stat.S_IRWXG − Read, write and execute by the group.
stat.S_IRGRP − Read by a group.
stat.S_IWGRP − Write by a group.
stat.S_IXGRP − Execute by a group.
stat.S_IRWXO − Read, write and execute by others.
stat.S_IROTH − Read by others.
stat.S_IWOTH − Write by others.
stat.S_IXOTH − Execute by others.
Example:
import os
# Set a file write by others.
os.chmod("/test/filename.txt", stat.S_IWOTH)