FILE HANDLING IN PYTHON

  • By
  • October 1, 2022
  • Python
File Handling in Python

FILE HANDLING IN PYTHON

Python supports file handling where users allow to handle their files for reading the data writing the data along with more options for operating the files. 

This concept was started in other languages but while implementing this will get complicated or a very long process .  but with help of python, this file handling becomes an easy and very small way. 

Python treats files like binary formatted files or text files in different styles. Where both formats are very important. in this process, each line of code contains a sequence of letters or we can say characters and finally, collections are called text files. Where each line of file/document gets terminated with a very special character called End Of line like comma (,) or it goes for the newline character. It ends with the current line and tells the interpreter to start with a new beginning. Python Training in Pune is the best certification course to acquire the latest trends in Python programming.

Let’s start to work with file operations :

  1. Open ()

Before starting with file operations, we need to create a file for that we need to open the file first. For that, we need to check is it already open or if we need to create it. after that when we start 

A process for opening while with an operation with help of applying mode.

Syntax :

F = open(filename,mode)

There is the following mode which describes a few things are listed below:

r : it opens the existing file to read the data

w : it opens the file to write the data

a : for adding data in file append operation done

r+ : to read and write data in the file 

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

for example :

file = open(‘test.txt’, ‘r’)

for each in file:

    print (each)

 

read() :

there are many ways to read the data from file in python for that you need to apply method like file.read()

for example :

file = open(“file.txt”, “r”)

print (file.read())

 

another way to read data from file

file = open(“test.txt”, “r”)

print (file.read(5))

 

write() :

to write the data in file following is the way 

file = open(‘test.txt’,’w’)

file.write(“This is the write command”)

file.write(“It allows us to write in a particular file”)

file.close()

here the close command terminates all the resources which are active.

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Append() :

file = open(‘test.txt’,’a’)

file.write(“This will add this line”)

file.close()

example :

with open(“test.txt”) as file: 

    data = file.read()

or another way 

with open(“test.txt”, “w”) as f:

    f.write(“Hello World!!!”)

 

split()

we can separate lines in file handling with python 

with open(“test.text”, “r”) as file:

    data = file.readlines()

    for line in data:

        word = line.split()

        print (word)

seek()

this method is used to change file handle situations to a specific location. where cursor defines data need to read or need to write 

here we need to provide the position index of the first character in files that start from zero like an index of string.

Example :

f = open(“sample.txt”, “r”)

f.seek(11)

print(f.read())

 

tell()

it returns the current position of file from the beginning of file

example :

f = open(“sample.txt”, “r”)

# read first line

f.readline()

print(f.tell())

copy ()

you can copy the data from one file to another file by following way :

with help of shutil package :

import shutil

 

src_path = r”C:\python\1files\rt\it.txt”

dst_path = r”E:\demospy\2files\nt\fit.txt”

shutil.copy(src_path, dst_path)

print(‘Copied’)

 

rename()

we can modify our file name with help of this method :

import os

 

# Absolute path of a file

old_name = r”D:\1demos\filespq\reportsall\details789.txt”

new_name = r”F:\2demos\filesst\reportsfew\new_details123.txt”

 

# Renaming the file

os.rename(old_name, new_name)

 

delete()

with the remove method, you can delete the file 

import os

 

# remove file with absolute path

os.remove(r”G:\123demos\filesxtra\34sales_26.txt”)

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

Working with bytes :

Normally bytes consist of 8 bits storage which has 0 or 1 data. A byte can be interpreted in different ways like binary to octal, to hexadecimal, or decimal form. Where python stores files in form of bytes in the storage area.

When we open any file which is always opened in text form that is already decoded from byte to object strings.

But when we open a file on binary mode then it returns contents such as bytes which are having no objects without decoded form.

 Here is an example where we need to start to open the file in web mode 

bytes_data = b’\x214′

with open(“test3.txt”, “wb”) as fp:

   fp.write(bytes_data)

 

Author:-

Sneha Ranbhidkar
Call the Trainer and Book your free demo Class 

Call now!!! | SevenMentor Pvt Ltd.

© Copyright 2021 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*