| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [View Raw Code] [Original HTTPS Page] |
import os module
import osList of Avialable Functions
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
Example:
import os
#Get Name
name=os.name
print(name)Output:
nt
Example:
#import required module
import os
#Get Current Working directory
direct=os.getcwd()
print(direct)Output:
C:\\python3
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\\demoOutput:
C:\\python3 C:\\Users\\ravi\\Desktop\\demo
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')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
'''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')Syntax:
os.rmdir('folderName')Example:
#import module
import os
#Remove testFolder
os.rmdir('testFolder')Syntax:
os.removedirs(path)Example:
import os
os.removedirs('tst/testFolder')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)| Back | FazBrowse Home | New Git URL |