OS Module in Python Programming

Introduction to Python OS Module: The OS module in Python provides functions for interacting with the operating system. It offers many useful OS functions that are used to perform OS-based tasks and get related information about operating system. The OS comes under Python's standard utility modules. Lets Start:-


OS Module in Python Programming

To work with the OS module, we need to import the OS module:


    import os

1. os.name:  os.name: This function gives the name of the operating system dependent module imported. The following names have currently been registered: ‘posix’, ‘nt’, ‘os2’, ‘ce’, ‘java’ and ‘riscos’

Code:


    import os
print(os.name)


2. os.mkdir() : The os.mkdir() function is used to create new directory.

Code:


    import os
print(os.mkdir(path))


3. getcwd(): It returns the current working directory(CWD) of the file.

Code:


    import os
print(os.getcwd())



4. os.chdir(): The os module provides the chdir() function to change the current working directory.

Code:


    import os
print(os.chdir(path))


5. os.getpid(): Returns the real process ID of the current process.

Code:


    import os
print(os.getpid())
    

6. listdir(): This method in Python is used to get the list of all files and directories in the specified directory.

Code:


    import os
print(os.listdir(path))


7. os.environ: The os.environ value is known as a mapping object that returns a dictionary of the user’s environmental variables.

Code:


    import os
print(os.environ)


8. os.rmdir(): This method in Python is used to remove or delete a empty directory. OSError will be raised if the specified path is not an empty directory.

Code:


    import os
print(os.rmdir(path))


9.  os.remove(): This method in Python is used to remove or delete a file path. This method can not remove or delete a directory. If the specified path is a directory then OSError will be raised by the method.

Code:


    import os
print(os.remove(path))


10. os.makedirs(): os.makedirs() method in Python is used to create a directory recursively.

Code:


    import os
print(os.makedirs(path))


11. os.system(): It is used to executing a shell command.

Code:


    import os
print(os.system)


12. os.walk(): The os.walk() method gives us a way to iterate over a root level path. It means that we can pass a path to this function and get access to all its sub-directories and files. 

Code:


    import os
print(os.walk(path))


13. os.error(): The os.error() function defines the OS level errors. It raises OSError in case of invalid or inaccessible file names and path etc.


14. os.popen(): This method opens a pipe to or from command. The return value can be read or written depending on whether mode is ‘r’ or ‘w’.

Code:


    import os
print(os.popen(fd,'w'))


15. os.access(): This function uses real uid/gid to test if the invoking user has access to the path.

Code:


    import os
print(os.name)


16. os.rename(old.txt, new.txt): A file old.txt can be renamed to new.txt, using the function os.rename(). The name of the file changes only if, the file exists and user has sufficient privilege permission to change the file.

Code:


    import os
print(os.rename("old.txt","new.txt"))


17. os.startfile(): The os.startfile() method allows us to “start” a file with its associated program. In other words, we can open a file with it’s associated program, just like when you double-click a PDF and it opens in Adobe Reader.

Code:


    import os
print(os.startfile(path))


18. os.close(): This function closes the associated file with descriptor fr.

Code:


    import os
print(os.close(file))


19. os.path: The os.path sub-module of the os module has lots of great functionality built into it.
  1. basename
  2. dirname
  3. exists
  4. isdir and isfile
  5. join
  6. split

1. os.path.basename
The basename function will return just the filename of a path. 

Code:


    import os
print(os.path.basename(path))


2. os.path.dirname
The dirname function will return just the directory portion of the path.

Code:


    import os
print(os.path.dirname(path))


3. os.path.exists
The exists function will tell you if a path exists or not. All you have to do is pass it a path.

Code:


    import os
print(os.path.exists(path))


4. os.path.join
The join method give you the ability to join one or more path components together using the appropriate separator.

Code:


    import os
print(os.join(path,path))


5. os.path.split
The split method will split a path into a tuple that contains the directory and the file.

Code:


    import os
print(os.path.split(path))


6. os.path.isdir / os.path.isfile
isdir only checks if the path is a directory and isfile only checks if the path is a file.

Code:


    import os
print(os.path.isdir(path))
    
    print(os.path.isfile(path))




Previous Post
Next Post

Hello, I am Ashish Yadav. Youtuber | Blogger | Programmer | Digital Marketer. Working on 3 Youtube Channel: 1. Expo Ashish 2. Expo Facts 3. Best Code Creator

Related Posts

Do Subscribe My Youtube Channel