[go: up one dir, main page]

0% found this document useful (0 votes)
95 views3 pages

FILE HANDLING 8th PROGRAM

The document discusses file handling in Python including opening, reading, writing, appending, and closing files. It shows how to open files in different modes, read and write content to files, and loop through files to read line by line. Examples count characters in a file and reverse the order when printing lines.

Uploaded by

CS BCA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views3 pages

FILE HANDLING 8th PROGRAM

The document discusses file handling in Python including opening, reading, writing, appending, and closing files. It shows how to open files in different modes, read and write content to files, and loop through files to read line by line. Examples count characters in a file and reverse the order when printing lines.

Uploaded by

CS BCA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

8.

FILE HANDLING

#open the file file.txt in read mode

fileptr=open("file.txt","r")

if fileptr:

print("file is opened successfully")

#Read file.txt using with statement

with open("file.txt",'r')as f:

content=f.read();

print(content)

#open the file.txt in append mode. create a new file if no such file exists

fileptr=open("file2.txt","w")

#appending the content to the file

fileptr.write('''''python is the modern day language. it makes things so simple it is the faster-growing
programming language''')

#closing the opened the file

#fileptr.close()

#open the file.txt in write mode

fileptr=open("file2.txt","a")

#Overwritting the content of the file

fileptr.write("python has an easy syntax and user-friendly interaction.")

#open the file.txt in read mode.causes an error if no such file exists

fileptr=open("file2.txt","r");

#running a for loop

for i in fileptr:

print(i)#i contains each line of the file

#closeing the opend file

fileptr.close()

OUTPUT
Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license()" for more information.

=================== RESTART: E:/phython lab/FileHandling1.py ===================

file is opened successfully

jfhhgkljll;;h

''python is the modern day language. it makes things so simple it is the faster-growing programming
languagepython has an easy syntax and user-friendly interaction.

2) FILE HANDLING

#Count the total number of upper case, lower case, and digits used in the

#text file "merge.txt"

def filehandling():

with open("merge.txt","r")as f1:

data=f1.read()

cnt_ucase=0

cnt_lcase=0

cnt_digits=0

for ch in data:

if ch.islower():

cnt_lcase+=1

if ch.isupper():

cnt_ucase+=1

if ch.isdigit():

cnt_digits+=1

print("Total Number of upper case letters are:",cnt_ucase)

print("Total Number of lower case letter are:",cnt_lcase)

print("Total Number of digits are:",cnt_digits)

filehandling();
def program11():

for i in reversed(list(open("merge.txt","r"))):

print(i. rstrip())

program11()

OUTPUT:

Python 3.10.6 (tags/v3.10.6:9c7b4bd, Aug 1 2022, 21:53:49) [MSC v.1932 64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license()" for more information.

====================== RESTART: E:/dharani/filehandling.py =====================

Total Number of upper case letters are: 5

Total Number of lower case letter are: 21

Total Number of digits are: 1

Msc.,CS 2nd year

M.DharaneeshwarI

You might also like