0% found this document useful (0 votes)
13 views

OS MODULE in File Handling

The document discusses how the os module in Python can be used for various file handling operations like manipulating file paths, retrieving file information, listing directories, changing file permissions, and working with environment variables. Some key functions of the os module include os.path.join() for creating file paths, os.listdir() for listing directory contents, os.path.getsize() for getting a file size, and os.getenv() for retrieving environment variables. The os module enhances code portability across operating systems by abstracting away file system details.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

OS MODULE in File Handling

The document discusses how the os module in Python can be used for various file handling operations like manipulating file paths, retrieving file information, listing directories, changing file permissions, and working with environment variables. Some key functions of the os module include os.path.join() for creating file paths, os.listdir() for listing directory contents, os.path.getsize() for getting a file size, and os.getenv() for retrieving environment variables. The os module enhances code portability across operating systems by abstracting away file system details.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

RSK INTERNATIONAL SCHOOL (CBSE)

(SENIOR SECONDARY)
CBSE Affiliation Number 1930766
COMPUTER SCIENCE - 083
GRADE: 12 TOPIC : OS MODULE (FILE HANDLING)

The os module in Python is primarily used for interacting with the operating system, and it provides
various functions related to file handling. Here are some common scenarios where you might use the os
module in the context of file handling:

1. File and Directory Operations:


 os.path submodule provides functions for common pathname manipulations.
 os.path.join() is useful for creating file paths by joining directory and file names.
 os.makedirs() can be used to create directories recursively.
 os.remove() and os.unlink() can be used to delete a file.
 os.rmdir() can be used to remove an empty directory.
 os.removedirs() can be used to remove empty directories recursively.
2. File Information:
 os.path.getsize() retrieves the size of a file.
 os.path.getmtime() retrieves the last modification time of a file.
 os.path.isfile() and os.path.isdir() check whether a given path points to a file or directory,
respectively.
 os.path.exists() checks whether a file or directory exists.
3. Directory Listing:
 os.listdir() can be used to get the list of files and directories in a given directory.
 os.walk() generates the file names in a directory tree.
4. File Permissions and Ownership:
 os.chmod() can be used to change the permissions of a file.
 os.chown() can be used to change the owner and group id of a file.
5. Working with Paths:
 os.getcwd() returns the current working directory.
 os.chdir() changes the current working directory.
 os.path.abspath() can be used to get the absolute path of a file or directory.
6. Environment Variables:
 os.environ provides a dictionary-like interface to the environment variables.
 os.getenv() retrieves the value of an environment variable.

Using the os module can enhance the portability of your code across different operating systems, as it
abstracts away the underlying details of the file system. It's a versatile module that provides a lot of
functionality for working with files and directories at the operating system level.

You might also like