10. File I/O

File

Python too supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files. The concept of file handling has stretched over various other languages, but the implementation is either complicated or lengthy, but a like other concepts of Python, this concept here is also easy and short. Python treats file differently as text or binary and this is important. Each line of code includes a sequence of characters and they form text file. Each line of a file is terminated with a special character, called the EOL or End of Line characters like comma {,} or newline character. It ends the current line and tells the interpreter a new one has begun.

open() Method :

The key function for working with files in Python is the open() function.

The open() function takes two parameters; filename, and mode.

There are four different modes for opening a file:

"r" - Read - Default value. Opens a file for reading, error if the file does not exist

"a" - Append - Opens a file for appending, creates the file if it does not exist

"w" - Write - Opens a file for writing, creates the file if it does not exist

"x" - Create - Creates the specified file, returns an error if the file exists

The open() function returns a file object, which has a read() method for reading the content of the file:

For Example:

def readFile():

    file = open("abc.txt") #Open a file

    text = file.read() # Read all data

    print(text)

    file.close() # Close a file

readFile()

Output:

Hello your working on File I/O

read()Method:

The read() method reads a string from an open file. It is important to note that Python strings can have binary data. apart from text data.

For Example:

f = open("abc.txt", "r")

print(f.read()) # read all file data

f = open("abc.txt", "r")

print(f.read(5)) #returns the five characters of file

f = open("abc.txt", "r")

print(f.readline()) #read one line of file

Note:When You run this program it will read all data according to method

close():

Close the file when you are work is finished

For Example:

f = open("abc.txt", "r")

print(f.readline())

f.close()

OS Module

It is possible to automatically perform many operating system tasks. The OS module in Python provides functions for creating and removing a directory (folder), fetching its contents, changing and identifying the current directory, etc.

mkdir() Method:

We can create a new directory using the mkdir() function from the OS module.

import os

os.mkdir("D:\\Movies")

the folder Movies created in D directory.

rmdir() Method:

we can remove the given path directory.

import os

os.rmdir("D:\\Movies")

listdir() Method :

The listdir() function returns the list of all files and directories

import os

os.listdir("D:\\")

remove() Method:

You can use the remove() method to delete files

import os

os.remove("D:\\abc.txt")

rename() Method:

The rename() method takes two arguments, the current filename and the new filename.

For Example:

import os

# Rename a file from abc.txt to xyz.txt

os.rename( "abc.txt", "xyz.txt" )