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

G 1: To Read Total Data From The File

The document contains examples demonstrating various methods for reading, writing, and modifying files in Python. It shows how to open files, write strings and lists of strings to files, read the entire file or specific portions, read line by line, get and set the file position cursor, modify file contents, and check if a file exists before opening.

Uploaded by

sathya priya
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)
48 views

G 1: To Read Total Data From The File

The document contains examples demonstrating various methods for reading, writing, and modifying files in Python. It shows how to open files, write strings and lists of strings to files, read the entire file or specific portions, read line by line, get and set the file position cursor, modify file contents, and check if a file exists before opening.

Uploaded by

sathya priya
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/ 4

f=open("abc.

txt",'w')

print("File Name: ",f.name)


print("File Mode: ",f.mode)
print("Is File Readable: ",f.readable())
print("Is File Writable: ",f.writable())
print("Is File Closed : ",f.closed)
f.close()
print("Is File Closed : ",f.closed)

f=open("abcd.txt",'w')
f.write("Durga\n")
f.write("Software\n")
f.write("Solutions\n")
print("Data written to the file successfully")
f.close()

f=open("abcd.txt",'w')
list=["sunny\n","bunny\n","vinny\n","chinny"]
f.writelines(list)
print("List of lines written to the file successfully")
f.close()

g 1: To read total data from the file


f=open("abc.txt",'r')
data=f.read()
print(data)
f.close()
g 2: To read only first 10 characters:
f=open("abc.txt",'r')
data=f.read(10)
print(data)
f.close()

Eg 3: To read data line by line:

f=open("abc.txt",'r')
ine1=f.readline()
print(line1,end='')
line2=f.readline()
print(line2,end='')
line3=f.readline()
print(line3,end='')
f.close()

Eg 4: To read all lines into list:

f=open("abc.txt",'r')
lines=f.readlines()
for line in lines:
print(line,end='')
f.close()

Eg 5:
f=open("abc.txt","r")
print(f.read(3))
print(f.readline())
print(f.read(4))
print("Remaining data")
print(f.read())
with open("abc.txt","w") as f:
f.write("Durga\n")
f.write("Software\n")
f.write("Solutions\n")
print("Is File Closed: ",f.closed)
print("Is File Closed: ",f.closed)

f=open("abc.txt","r")
print(f.tell())
print(f.read(2))
print(f.tell())
print(f.read(3))
print(f.tell())

data="All Students are intelligences"


f=open("abc.txt","w")
f.write(data)
with open("abc.txt","r+") as f:
text=f.read()
print(text)
print("The Current Cursor Position: ",f.tell())
f.seek(17)
print("The Current Cursor Position: ",f.tell())
f.write("GEMS!!!")
f.seek(0)
text=f.read()
print("Data After Modification:")
print(text)

import os,sys
fname=input("Enter File Name: ")
if os.path.isfile(fname):
print("File exists:",fname)
f=open(fname,"r")
else:
print("File does not exist:",fname)
sys.exit(0)
print("The content of file is:")
data=f.read()
print(data)
import pdb
var=10
pdb.set_trace()
a=int(input("enter the a string"))
b=int(input("enter the b value"))
sum=a+b
print(sum)
def add(a, b):
return a + b
def samplefunc():
var = 10
print("One. Line 1 ..")
print("Two. Line 2 ..!")
out = add(20, var)
print('Three. Line 3 .. Done!')
return out
print(samplefunc())

You might also like