Skip to content

Commit

Permalink
practical file
Browse files Browse the repository at this point in the history
  • Loading branch information
rakeshlinux committed Jan 23, 2021
1 parent e179462 commit a571c89
Show file tree
Hide file tree
Showing 17 changed files with 363 additions and 0 deletions.
9 changes: 9 additions & 0 deletions PracticalFileQuestion/Q12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Write a program in python, to create two number lists a and b .
# Swap and display all elements of both lists.

list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, 14, 15, 16, 18, 20]
list2= [13,14,15,18,34,45,56,768,89,890,78]

list1, list2 = list2,list1
print(list1)
print(list2)
11 changes: 11 additions & 0 deletions PracticalFileQuestion/Q13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Write a UDF in python, it will take two arguments list
# (sequence of elements) and its size .
# Replace and display first half elements with second half elements of a list.
# For example: list elements are: 1 2 3 4 5
# Output is: 4 5 3 1 2

list1 = [1,2,3,4,5]
n = len(list1)
for i in range(n//2):
list1[i],list1[n//2+1+i]= list1[n//2+1+i],list1[i]
print(list1)
19 changes: 19 additions & 0 deletions PracticalFileQuestion/Q14.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Write a UDF in python, it will take two arguments list(sequence of elements)
# and its size. Function arrange and display elements in ascending order.
# Using selection sort.

def insertion_sort(list1):
n = len(list1)
for i in range(1,n):
temp = list1[i]
j = i-1
while j>=0 and temp<list1[j]:
list1[j+1]= list1[j]
j = j-1
list1[j+1]= temp
return list1

# function implementation
list1 = [43,3,1,23,45,45,6,7,8,89,34,78]
list1 = insertion_sort(list1)
print(list1)
15 changes: 15 additions & 0 deletions PracticalFileQuestion/Q15.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Write a function in PYTHON that counts the number of “Me” or “My” words present in a
# text file “DIARY.TXT”. If the “DIARY.TXT” contents are as follows:
# My first book was Me and My Family. It gave me chance to be Known to the world.
# The output of the function should be:
# Count of Me/My in file: 4

def count_me_my():
file = open('C:/python/PracticalFileQuestion/diary.txt', 'r')
data = file.read().split()
count = data.count('Me') +data.count('me') + data.count('My')+data.count('my')
file.close()
print('Count of me or My :',count)

#function call
count_me_my()
14 changes: 14 additions & 0 deletions PracticalFileQuestion/Q16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#Write a function in PYTHON to read the contents of a text file “Places.Txt”
# and display all those lines on screen which are either starting with ‘P’ or with ‘S’.
def line_count():
file = open('C:/python/PracticalFileQuestion/diary.txt', 'r')
count =0
for line in file.readlines():
if line[0]=='P' or line[0]=='S':
count+=1
print(line)
file.close()
print('Total lines :',count)

#function implementation
line_count()
15 changes: 15 additions & 0 deletions PracticalFileQuestion/Q17.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Write a function EUCount() in PYTHON, which should read each character
# of a text file IMP.TXT, should count and display the occurrences of
# alphabets E and U (including small cases e and u too).

def EUcount():
file = open('C:/python/PracticalFileQuestion/diary.txt', 'r')
count = 0
for x in file.read():
if x in 'EeUu':
count+=1
file.close()
print('Total E or U in file :',count)

#function implementation
EUcount()
20 changes: 20 additions & 0 deletions PracticalFileQuestion/Q18.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Write a function in PYTHON to search for a BookNo from a binary file
# “BOOK.DAT”, assuming the binary file is containing the records of the
# following type: ("BookNo", "Book_name").
# Assume that BookNo is an integer.
import pickle

tno = int(input('Enter book no to search :'))
file = open('C:/python/PracticalFileQuestion/book.dat', 'rb')
found=0
while True:

try:
data = pickle.load(file)
if data['bookno']==tno:
found=1
print(data)
except:
break
file.close()
print("Book found" if found==1 else "Book Not Found")
17 changes: 17 additions & 0 deletions PracticalFileQuestion/Q18_pre.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#program to create a binary file having records of book in the format
# {'bookno','book_name'}
import pickle

file = open('C:/python/PracticalFileQuestion/book.dat', 'ab')
book ={}
while True:
no = int(input('Enter book no :'))
name = input('Enter book no :')
book['bookno'] = no
book['book_name'] = name
pickle.dump(book,file)
choice = input('Add more books(y/n) :').upper()
if choice =='N':
break
file.close()
print('Binary file creation complete.....')
38 changes: 38 additions & 0 deletions PracticalFileQuestion/Q19_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Write a function in Python for PushS(List) and for PopS(List) for performing
# Push and Pop operations with a stack of List containing integers.

def push(stack):
value = int(input('Enter any integer no :'))
stack.append(value)

def pop(stack):
if len(stack)<=0:
print('\n Stack Underflow')
else:
print('\nPoped value from stack :',stack.pop())

def display(stack):
print('\n\nStack Elements : ')
for x in stack:
print(x, end=' ')

#implementation of function to show stack
stack=[]
while True:
print('\n STACK MENU')
print('1. Push')
print('2. Pop')
print('3. Display ')
print('4. Exit')
choice = input('Enter your choice :')
if choice=='1':
push(stack)
if choice=='2':
pop(stack)
if choice=='3':
display(stack)
if choice=='4':
break



39 changes: 39 additions & 0 deletions PracticalFileQuestion/Q20_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Write a function in Python for PushS(List) and for PopS(List) for performing
# Push and Pop operations with a queue of List containing integers.


def insert_element(queue):
value = int(input('Enter any integer no :'))
queue.append(value)


def delete_element(queue):
if len(queue) <= 0:
print('\n Queue Underflow')
else:
print('\nDeleted Elment from from Queue :', queue.pop(0))


def display(queue):
print('\n\nQueue Elements : ')
for x in queue:
print(x, end=' ')


#implementation of function to show queue
queue = []
while True:
print('\n QUEUE MENU')
print('1. Insert Element')
print('2. Delete Element')
print('3. Display ')
print('4. Exit')
choice = input('Enter your choice :')
if choice == '1':
insert_element(queue)
if choice == '2':
delete_element(queue)
if choice == '3':
display(queue)
if choice == '4':
break
55 changes: 55 additions & 0 deletions PracticalFileQuestion/Q21_csv_file_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Write a menu-driven program implementing user-defined functions to perform
# different functions on a csv file “student” such as:
# (a) Write a single record to csv.
# (b) Write all the records in one single go onto the csv.
# (c) Display the contents of the csv file.
import csv

def single_record():
file = open('C:/python/PracticalFileQuestion/student.csv', 'a')
admno = int(input('Enter admno :'))
name = input('Enter name :')
std = input('Enter standard :')
writer = csv.writer(file,lineterminator='\n')
writer.writerow([admno,name,std])
file.close()


def multiple_records():
file = open('C:/python/PracticalFileQuestion/student.csv', 'a')
data=[]
while True:
admno = int(input('Enter admno :'))
name = input('Enter name :')
std = input('Enter standard :')
data.append([admno,name,std])
choice= input('Add more records(y/n) :').upper()
if choice =='N':
break

writer = csv.writer(file, lineterminator='\n')
writer.writerows(data)
file.close()

def read_csv_file():
file = open('C:/python/PracticalFileQuestion/student.csv', 'r')
reader = csv.reader(file)
for line in reader:
print(line)
file.close()

while True:
print('\n\n CSV File Handling')
print('1. Write Single Record')
print('2. Write multiple Records')
print('3. Read Whole CSV file ')
print('4. Close Application')
choice=input('Enter your choice :')
if choice=='1':
single_record()
if choice=='2':
multiple_records()
if choice=='3':
read_csv_file()
if choice=='4':
break
102 changes: 102 additions & 0 deletions PracticalFileQuestion/Q22_binary_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Write a menu-driven program to perform all the basic operations using dictionary on student
# binary file such as inserting,reading, updating, searching and deleting a record.

import pickle
import os


def insert_record():
file = open('C:/python/PracticalFileQuestion/binary.dat', 'ab')
admno = int(input('Enter admno :'))
name = input('Enter name :')
std = input('Enter standard :')
student={'admno':admno,'name':name,'std':std}
pickle.dump(student,file)
file.close()
print('Record added successfully........')


def read_records():
file = open('C:/python/PracticalFileQuestion/binary.dat', 'rb')
while True:
try:
data = pickle.load(file)
print(data)
except:
break
file.close()


def delete_record():
file = open('C:/python/PracticalFileQuestion/binary.dat', 'rb')
temp = open('temp.dat', 'wb')
tadmno = int(input('Enter admission no to delete :'))
while True:
try:
data = pickle.load(file)
if data['admno'] != tadmno:
pickle.dump(data, temp)
except:
break
file.close()
temp.close()
os.remove('C:/python/PracticalFileQuestion/binary.dat')
os.rename('temp.dat', 'C:/python/PracticalFileQuestion/binary.dat')
print('Record updated......')


def update_record():
file = open('C:/python/PracticalFileQuestion/binary.dat', 'rb')
temp = open('temp.dat','wb')
tadmno = int(input('Enter admission no to update :'))
while True:
try:
data = pickle.load(file)
if data['admno']==tadmno:
data['name'] = input('Enter new name :')
data['std'] = input('Enter new standard :')
pickle.dump(data,temp)
except:
break
file.close()
temp.close()
os.remove('C:/python/PracticalFileQuestion/binary.dat')
os.rename('temp.dat', 'C:/python/PracticalFileQuestion/binary.dat')
print('Record updated......')


def search_record():
file = open('C:/python/PracticalFileQuestion/binary.dat', 'rb')
tadmno = int(input('Enter admno to search :'))
found=0
while True:
try:
data = pickle.load(file)
if data['admno']==tadmno:
found =1
except:
break
file.close()
print('Record found ' if found==1 else 'Record not found ')

while True:
print('\n\n Binary File Handling')
print('1. Insert Record')
print('2. Delete Records')
print('3. Update Record ')
print('4. Search Record ')
print('5. Read all Records ')
print('6. Close Application')
choice = input('Enter your choice :')
if choice == '1':
insert_record()
if choice == '2':
delete_record()
if choice == '3':
update_record()
if choice == '4':
search_record()
if choice== '5':
read_records()
if choice== '6':
break
Binary file added PracticalFileQuestion/binary.dat
Binary file not shown.
Binary file added PracticalFileQuestion/book.dat
Binary file not shown.
4 changes: 4 additions & 0 deletions PracticalFileQuestion/diary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
My first book was Me and My Family.
It gave me chance to be Known to the world.
Passing criteria is not always same
Same if the line contains p or s
4 changes: 4 additions & 0 deletions PracticalFileQuestion/student.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1,rakesh,12
2,anmol,11
3,subodh,10
4,ravi,10
1 change: 1 addition & 0 deletions PracticalFileQuestion/tempCodeRunnerFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
temp = list1[i]

0 comments on commit a571c89

Please sign in to comment.