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

Python Practical Programs

The document contains Python programs for file operations, including copying contents from one file to another, inserting or appending records in a binary file using pickle, and reading records from a binary file. The first program reads a source file and writes its contents to a destination file, while the second program allows user input to create student records and save them in a binary format. The final part reads and displays the contents of a binary file containing student records.

Uploaded by

gamingapex004
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)
11 views3 pages

Python Practical Programs

The document contains Python programs for file operations, including copying contents from one file to another, inserting or appending records in a binary file using pickle, and reading records from a binary file. The first program reads a source file and writes its contents to a destination file, while the second program allows user input to create student records and save them in a binary format. The final part reads and displays the contents of a binary file containing student records.

Uploaded by

gamingapex004
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

#program to copy the contents of a file to another file

import os

def filecopy(gourav,FILE2):

f1 = open(gourav, 'r')

f2 = open(FILE2, 'w')

line = [Link]()

while line != '':

[Link](line) #write the line from f1 with additional newline

line = [Link]()

[Link]()

[Link]()

def main():

fileName1= input('source file name: ')

fileName2=input('destination file name : ')

filecopy(fileName1, fileName2)

if __name__ == '__main__':

main()
#program for inserting or appending a record in a binary file student

import pickle

record = []

while True :

roll_no = int(input("enter the student roll no :"))

name = input("enter the student name")

marks = int(input("ENTER THE MARKS OBTAINED"))

DATA = [roll_no, name, marks]

choice = input("wish to enter more records (Y/N)?: ")

if [Link]() == 'N':

break

f=open("student" , "wb")

[Link](record, f)

print("record added")

[Link]
#read a record from the binaery file [Link]

import pickle

f = open("[Link]" , "rb")

stud_rec = [Link](f)

print("contents of student file are:")

#read the fields from the file

for R in stud_rec:

roll_no = R[0]

name = R[1]

marks = [2]

print(roll_no, name, marks)

[Link]

You might also like