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

Project 2.0

The document discusses various methods for reading and writing files in Python, including reading the entire file, specific portions of the file, line by line, and into lists. It also covers writing strings, numbers, and lists to files, opening files in different modes, reading and writing binary files, and copying file contents. The code examples demonstrate how to perform common file input/output operations in Python.

Uploaded by

Mohit Kumar
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)
122 views

Project 2.0

The document discusses various methods for reading and writing files in Python, including reading the entire file, specific portions of the file, line by line, and into lists. It also covers writing strings, numbers, and lists to files, opening files in different modes, reading and writing binary files, and copying file contents. The code examples demonstrate how to perform common file input/output operations in Python.

Uploaded by

Mohit Kumar
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/ 19

To illustrate read by reading the entire data from a file.

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()

for line in lines:

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.write("We are writing \n")

f.write("text file \n")

print("Data written to the file successfully")

f.close()

…………………..
Program to write numeric data to file.

f=open("newtext.txt","w")

x=100

f.write("Hello World \n")

f.write(str(x))

f.close()

………………….
Program to add list items to file using writelines method.

f=open("text4.txt","w")

list=["computer science \n","Physics\n","Physics\n","Chemistry\n","Maths"]

f.writelines(list)

print("List of lines written to the file successfully")

f.close()

……………………
Program to illustrate with statement.

with open("test1.txt","w") as f:

f.write("Python \n")

f.write("is an easy \n")

f.write("language \n")

f.write("to work with \n")

print("Is file closed :",f.closed)

print("Is file closed: ",f.close())

………………………………….
Program to add data to existing file.

f=open("text.txt","a")

f.write('Simple syntax of the language \n')

f.write("makes Python program easy to read and write")

print("More Data appended to the file")

f.close()

………………………………….
Code a program that asks the user to input their names
along with age and store the data in a text file.

name=input("Enter your name:")

age=input("Enter your age:")

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)

print("File added to binary file")

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:

x=int(input("Enter the integer:"))

pickle.dump(x,file)

ans=input("Do you want to enter more data Y/N:")

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()

………………………………………………………………………………………………………………………….

Program to implement standard streams.

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()

while line!=' ':

f2.write(line)

line=f1.readline()

f1.close()

f2.close()

def main():

filename1=input('Enter the source file name:')

filename2=input('Enter the destination file name: ')

filecopy(filename1,filename2)

if __name__=='__main__':
main()

…………………………………………….,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

Write a function capitalize sentence to create a copy of


file which would convert the first letter of file and the
first alphabetic character into upper case.

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:

print("source file does not exist")

f1.close()

f2.close()

capitalize_Sentence()

………………………………………………………………………………………………………………………………..

You might also like