import os => Import moduleos.mkdir("folderPath") => Creates a folder in the current directoryvarName = open("filePath", os.O_RDONLY) => Open file in read only modevarName = open("filePath", os.O_WRONLY) => Open file in write only modevarName1 = os.read(varName, 1024) => Read contents of the fileos.close() => Close the fileos.path.exists("filePath") => Returns true of file/folder existsos.rename("sourceFilePath", "destinationFilePath") => Renamesos.listdir("folderPath") => Returns list of foldersos.getcwd()os.system("date")varName = open("fileName", "r") => Open file in reading mode, Gives error if file does not existvarName = open("fileName") => Default mode is readingvarName1 = varName.read() => Gets content of the filevarName = open("fileName", "w") => Open file in writing mode, Creates a new file if it does not existvarName.write("value") => Write in file, Needs to close the filevarName = open("fileName", "a") => Appending in file, Creates a new file if it does not existvarName.write("value") => Appends in filevarName = open("fileName", "x") => Creates file, Gives error if it already existvarName = open("fileName", "rt") => Used to handle text file, Default is this modevarName = open("fileName", "wt")varName = open("fileName", "rb") => Used to handle binary files, Images, PDFsvarName.close() => Closes the opened file with open("fileName", "w") as f:
f.write("value")
f.truncate(N) # Truncates the file to specified size
with open("fileName", "r") as f:
f.seek(N) # Move to Nth byte in file
varName = f.read(N) # Read the next N bytes
f.tell() # Returns the current position within the file in bytes