OS module in python - Python for AI

Build Python AI tools with practical LLM tutorials. Learn to rewrite search queries, classify text, and use local models in your own projects.

OS module in python

The OS module provides a big range of useful method to manipulate files and directories. Some of the useful and commonly used methods are:

  • os.access(path,mode): use to check the access to path. It returns true if access is allowed and False if not allowed. Where modes could one of these:
    1. os.F_OK: to test existence of path
    2. os.R_OK: to test readability access to path
    3. os.W_OK: to test writ-ability access to path
    4. os.X_OK: to test if path can be executed or not. 

  • os.cwd(): return the current working directory

  • os.chdir(path): changes the current working directory to the given path. It returns none in all the cases.

  • os.mkdir(dir_name): creates a directory inside the current working directory with the name passed as argument inside parenthesis.

  • os.mkdirs(folder1/folder2...): it is used when we want to create a tree structure of the directories inside our current working directories.

  • os.rmdir(dir_name): removes the directory passed in as argument.

  • os.removedirs(folder1/folder2...): this comes in use when we are about to remove a tree structure of folders(directories).

  • os.rename(old_name,to_name): use to rename the files and folders

  • os.stat(arg): provide th e details about file or folder passed as arguments.

  • os.walk(root_directory): retrieves the tree structure inside root_directoory. It fetch value in three sequence:
    1. dir_path: which is the path of the directory
    2. Name_Of_the_Directory
    3. Files or folders inside that directory

  •  os.path.split(address): returns the last name of file or folder in the address

  • os.path.splitext(file_name): returns the extension of file.

No comments:

Post a Comment