Python OS Module
Python OS module provides the facility to establish the interaction between the user and 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. This module offers a portable way of using operating system dependent functionality.
The Python OS module lets us work with the files and directories.
There are some functions in the OS module which are given below:
This function provides the name of the operating system module that it imports.
Currently, it registers 'posix', 'nt', 'os2', 'ce', 'java' and 'riscos'.
The os.mkdir() function is used to create new directory. Consider the following example.
It will create the new directory to the path in the string argument of the function in the D drive named folder newdir.
It returns the current working directory(CWD) of the file.
The os module provides the chdir() function to change the current working directory.
The rmdir() function removes the specified directory with an absolute or related path. First, we have to change the current working directory and remove the folder.
The os.error() function defines the OS level errors. It raises OSError in case of invalid or inaccessible file names and path etc.
Problem reading: Python.txt
This function opens a file or from the command specified, and it returns a file object which is connected to a pipe.
This is awesome
This function closes the associated file with descriptor fr.
Traceback (most recent call last): File "main.py", line 3, in file = open(fr, 'r') FileNotFoundError: [Errno 2] No such file or directory: 'Python1.txt'
A file or directory can be renamed by using the function os.rename(). A user can rename the file if it has privilege to change the file.
Traceback (most recent call last): File "main.py", line 3, in os.rename(fd,'Python1.txt') FileNotFoundError: [Errno 2] No such file or directory: 'python.txt' -> 'Python1.txt'
This function uses real uid/gid to test if the invoking user has access to the path.
Exist path: False It access to read the file: False It access to write the file: False Check if path can be executed: False
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week