0% found this document useful (0 votes)
10 views8 pages

Imp Programs

Uploaded by

tankvarma
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)
10 views8 pages

Imp Programs

Uploaded by

tankvarma
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/ 8

TEXT FILE

#CREATING AND DISPLAYING TEXT FILE

def create_file(content):

with open('stu.txt', 'w') as file:

file.write(content)

print("file is created")

def display_file():

with open('stu.txt', 'r') as file:

print('=============================================')

print(file.read())

print('=============================================')

info=input('enter the data to be added to file')

create_file(info)

display_file()

'''

enter the data to be added to file i am tanmay of class 12 c

file is created

=============================================

i am Tanmay of class 12 c

=============================================

'''

#TO SEARCH IN TEXT FILE

def search_in_file(search_term):

with open('stu.txt', 'r') as file:

content = file.read()

if search_term in content:

print(search_term,'is present in file')

else:

print(search_term,'is not present in file')


search_in_file('tanmay')

'''

tanmay is present in file

'''

#TO APPEND TEXT FILE

def append_to_file(content):

with open('stu.txt', 'a') as file:

file.write(content)

print("added succesfully")

with open('stu.txt', 'r') as file:

print(file.read())

exinfo=input('enter info to be added further')

append_to_file(exinfo)

'''

enter info to be added further i love computer science

added succesfully

i am Tanmay of class 12 c i love computer science i love computer science

'''

#TO COPY TEXT FILE

def copy_file():

with open('stu.txt', 'r') as source_file:

content = source_file.read()

with open('stu2.txt', 'w') as destination_file:

destination_file.write(content)

print("file copied")

with open('stu2.txt', 'r') as file:

print(file.read())

copy_file()

'''

file copied
i am tanmay of class 12 c i love computer science i love computer science

'''

#DELETING A FILE

import os

def delete_file(filename):

if os.path.exists(filename):

os.remove(filename)

print(f"File '{filename}' deleted.")

else:

print(f"File '{filename}' does not exist.")

delete_file('stu2.txt')

'''

File 'stu2.txt' deleted.

'''

CSV FILE
#CREATE AND DISPLAY A CSV FILE

import csv

import os

def create_csv( headers, rows):

with open('stu.csv', 'w', newline='') as file:

writer = csv.writer(file)

writer.writerow(headers)

writer.writerows(rows)

print('csv file created')

with open('stu.csv', 'r', newline='') as file:

with open('stu.csv', 'r') as file:

reader = csv.reader(file)

for row in reader:

print(', '.join(row))

heading=['name','roll no','marks']

content=[
['prakhar',81,90],

['vivek',90,18],

create_csv(heading,content)

'''

csv file created

name, roll no, marks

Tanmay , 81, 90

vivek, 90, 18

'''

#TO APPEND IN CSV FILE

import csv

def append_csv(row):

with open('stu.csv', 'a', newline='') as file:

writer = csv.writer(file)

writer.writerow(row)

print('row appended')

with open('stu.csv', 'r') as file:

reader = csv.reader(file)

rows = list(reader)

print(rows)

append_csv(['reshma ','17','76'])

'''

[['name', 'roll no', 'marks'], ['mohit', '98', '50'], ['vivek', '90', '18'], ['reshma', '17', '76']]

'''

#TO MODIFY CSV FILE

import csv

def modify_csv(row_number, new_row):

rows = []

with open('stu.csv', 'r') as file:

reader = csv.reader(file)
rows = list(reader)

if 0 <= row_number < len(rows):

rows[row_number] = new_row

with open('stu.csv', 'w', newline='') as file:

writer = csv.writer(file)

writer.writerows(rows)

print('file is modified')

with open('stu.csv', 'r') as file:

reader = csv.reader(file)

rows = list(reader)

print(rows)

modify_csv(1,['mohit','98','50'])

'''

file is modified

[['name', 'roll no', 'marks'], ['mohit', '98', '50'], ['vivek', '90', '18']]

‘’’

#SEARCH IN CSV FILE

import csv

def search_csv(search_term):

with open('stu.csv', 'r') as file:

reader = csv.reader(file)

found = False

for row in reader:

if search_term in row:

found = True

else:

found = False

if found==True:

print(search_term,'is present in file')

else:

print(search_term,'is not present in file')


search_csv('vivek')

'''

======================

vivek is present in file

'''

#WAP TO DELETE CSV FILE

import os

import csv

def delete_csv(filename):

if os.path.exists(filename):

os.remove(filename)

print(f"CSV file '{filename}' deleted.")

else:

print(f"CSV file '{filename}' does not exist.")

delete_csv('stu2.csv')

'''

CSV file 'stu2.csv' does not exist.

'''

#BINARY FILE
#CREATE AND DISPLAY A BINARY FILE

import pickle

import os

def create_binary_file(filename, data):

with open(filename, 'wb') as file:

pickle.dump(data, file)

print(f"Binary file '{filename}' created.")

with open(filename, 'rb') as file:

data = pickle.load(file)

print(f"Content of '{filename}':")

print(data)
create_binary_file('test.dat','my name is Tanmay ')

'''

Binary file 'test.dat' created.

Content of 'test.dat':

my name is Tanmay

'''

#SEARCH IN A BINARY FILE

import pickle

import os

def search_binary_file(filename, search_item):

with open(filename, 'rb') as file:

data = pickle.load(file)

if search_item in data:

print(f"Item '{search_item}' found in the file.")

else:

print(f"Item '{search_item}' not found in the file.")

search_binary_file('test.dat', 'tanmay ')

'''

Item 'tanmay ' found in the file.

'''

#MODIFY A BINARY FILE

import pickle

import os

def modify_binary_file(filename, new_item):

with open(filename, 'wb') as file:

pickle.dump(new_item, file)

print('file modified')

with open(filename, 'rb') as file:

data = pickle.load(file)

print(f"Content of '{filename}':")

print(data)
modify_binary_file('test.dat','computers')

'''

file modified

Content of 'test.dat':

computers

'''

#APPEND A BINARY FILE

import pickle

import os

def append_to_binary_file(filename, item):

with open(filename, 'rb') as file:

data = pickle.load(file)

data=data+' '+item

with open(filename, 'wb') as file:

pickle.dump(data, file)

print(f"Item appended to '{filename}'.")

with open(filename, 'rb') as file:

data = pickle.load(file)

print(f"Content of '{filename}':")

print(data)

append_to_binary_file('test.dat','tanmay')

'''

Item appended to 'test.dat'.

Content of 'test.dat':

computers tanmay

'''

You might also like