Project 2.0
Project 2.0
f=open("text.txt","r")
data=f.read()
print(data)
f.close()
……………………….
To illustrate the reading only the first 10 characters from
file.
f=open("text.txt","r")
data=f.read(10)
print(data)
f.close()
………………………………………
To read the data line by line.
f=open("text.txt","r")
line1=f.readline()
print(line1,end='')
line2=f.readline()
print(line2,end='')
line3=f.readline()
print(line3,end='')
line4=f.readline()
print(line4,end='')
f.close()
………………………….
To read all the lines from the list.
f=open("text.txt","r")
lines=f.readlines()
print(line,end='')
f.close()
………………………………………….
Program to display the contents of the file starting from
2nd character into the list.
f=open("text.txt","r")
print(f.read(2))
print(f.readlines())
print(f.read(3))
print("Remaining Data")
print(f.read())
…………………………..
Program to write data to the file.
f=open("text.txt","w")
f.close()
…………………..
Program to write numeric data to file.
f=open("newtext.txt","w")
x=100
f.write(str(x))
f.close()
………………….
Program to add list items to file using writelines method.
f=open("text4.txt","w")
f.writelines(list)
f.close()
……………………
Program to illustrate with statement.
with open("test1.txt","w") as f:
f.write("Python \n")
f.write("language \n")
………………………………….
Program to add data to existing file.
f=open("text.txt","a")
f.close()
………………………………….
Code a program that asks the user to input their names
along with age and store the data in a text file.
f=open("user_details.txt","a")
f.write(name)
f.write(age)
f.close()
……………………………………………….
Program to write structure, list in binary file.
def foperation():
import pickle
list1=[10,20,30,40,100]
f=open('list.dat','wb')
pickle.dump(list1,f)
f.close()
foperation()
………………………………………
Program to write structure, dictionary to binary file.
import pickle
dict1={'Python':90,'Java':95, 'C++':85}
f=open('bin_file.dat','wb')
pickle.dump(dict1,f)
f.close()
…………………..
Program to read dictionary items from binary file .
import pickle
f=open('bin_file.dat','rb')
dict1=pickle.load(f)
f.close()
print(dict1)
…………………………
Program to store and display multiple integers in and
from a binary file.
def binfile():
import pickle
file=open('data.dat','wb')
while True:
pickle.dump(x,file)
if ans.upper()=='N': break
file.close()
file=open('data.dat','rb')
try:
while True:
y=pickle.load(file)
print(y)
except EOFError:
pass
file.close()
binfile()
………………………………………………………………………………………………………………………….
import sys
f1=open(r"text.txt")
line1=f1.readline()
line2=f1.readline()
line3=f1.readline()
sys.stdout.write(line1)
sys.stdout.write(line2)
sys.stdout.write(line3)
………………………………………………………………………………………………….
Program to copy contents of a file to another file.
import os
def filecopy(file1,file2):
f1=open(file1,'r')
f2=open(file2,'w')
line=f1.readline()
f2.write(line)
line=f1.readline()
f1.close()
f2.close()
def main():
filecopy(filename1,filename2)
if __name__=='__main__':
main()
…………………………………………….,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
def capitalize_Sentence():
f1=open("test_report.txt","r")
f2=open("file1.txt","w")
while 1:
line=f1.readline()
if not line:
break
line=line.rstrip()
linelength=len(line)
str=''
str=str+line[0].upper()
i=1
while i<linelength:
if line[i]==".":
str=str+line[i]
i=i+1
if i>=linelength:
break
str=str+line[i].upper()
else:
str=str+line[i]
i=i+1
f2.write(str)
else:
f1.close()
f2.close()
capitalize_Sentence()
………………………………………………………………………………………………………………………………..