All Courses

File Handling with OS Module

Reena Sen

a year ago

File Handling with OS Module | insideAIML
Table of Contents
  • Introduction
  • OS module
  • File Operation

Introduction

          In the software industry, most of the programs handle files in one way or another, such as creating files, renaming them, moving, listing, etc. As a programmer, you should definitely have this fundamental skill. As a result, in this tutorial, we will be using os module in Python to make several operations on files and directories regardless of our operating system we're using.  
The OS module in python provides functions for interacting with the operating system. OS, comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. The *os* and *os.path* modules include many functions to interact with the file system.
In addition to File object returned by open() function, file IO operations can also be performed using Python's built-in library has os module that provides useful operating system dependent functions. These functions perform low level read/write operations on file.
The open() function from os module is similar to the built-in open(). However, it doesn't return a file object but a file descriptor, a unique integer corresponding to file opened. File descriptor's values 0, 1 and 2 represent stdin, stdout, and stderr streams. Other files will be given incremental file descriptor from 2 onwards.

OS module

          As in case of open() built-in function, os.open() function also needs to specify file access mode. Following table lists various modes as defined in os module.
OS Module and Description | Insideaiml
To open a new file for writing data in it, specify O_WRONLY as well as O_CREAT modes by inserting pipe (|) operator. The os.open() function returns a file descriptor.

f=os.open("test.dat", os.O_WRONLY|os.O_CREAT)
Note that, data is written to disk file in the form of byte string. Hence, a normal string is converted to byte string by using encode() function as earlier.

data="Hello World".encode('utf-8')
The write() function in os module accepts this byte string and file descriptor.

os.write(f,data)
Don’t forget to close the file using close() function.

os.close(f)
To read contents of a file using os.read() function, use following statements:

f=os.open("test.dat", os.O_RDONLY)
data=os.read(f,20)
print (data.decode('utf-8'))
Note that, the os.read() function needs file descriptor and number of bytes to be read (length of byte string).

File Operation

          If you want to open a file for simultaneous read/write operations, use O_RDWR mode. Following table shows important file operation related functions in os module.
Functions and Description | Insideaiml
   
Enjoyed reading this blog? Then why not share it with others. Help us make this AI community stronger. 
To learn more about such concepts related to Artificial Intelligence, visit our insideAIML blog page.
You can also ask direct queries related to Artificial Intelligence, Deep Learning, Data Science and Machine Learning on our live insideAIML discussion forum.
Keep Learning. Keep Growing.

Submit Review