Professional Documents
Culture Documents
Filehandling
Filehandling
124
hello , everyone
my name is tariq ahmed
hello , everyone
hello , everyone
In [22]: #write a text in file and show output but using with.
with open("tariq.txt","w") as myfile:
txt="hi hi hi"
i=myfile.write(txt)
print(txt)
hi hi hi
Quiz: program to take input untill user not exit the program
and then those input (txt) write in txt file.
In [30]: myfile=open("asad.txt","w")
while True:
rollno=int(input("Enter a Roll No:"))
clas=input("Enter class :")
rollno=str(rollno)
myfile.write(rollno)
myfile.write(clas)
myfile.close()
Quiz: same question but with spirit comma or with new line
and with integer input. program to take input untill user not
exit the program and then those input (txt) write in txt file.
In [43]: p=open("asad.txt","w")
while True:
rollno=int(input("Enter a roll no:"))
marks=int(input("Enter marks"))
convrt=str(rollno)+","+str(marks)
p.write(convrt)
In [45]: f=open("new.txt","w")
record=[]
for i in range(5):
name=input("Enter a number :")
record.append(name)
#comma add
#record.append(name+",")
f.writelines(record)
print("file created...")
f.close()
append
In [3]: #append
p=open("asnaf.txt","a")
txt="khan"
wt=p.write(txt)
print(txt)
khan
In [7]: my_file=open("newfile.txt","a")
while True:
n=input("Enter a name: ")
f=input("Enter father name :")
new=n+f
my_file.write(new)
choice =input("Enter X to Exit Y to Continue :")
if choice =='x' or choice =='X':
break
print("file created...")
my_file.close()
small project
localhost:8888/nbconvert/html/Old Programming Concept/filehandling.ipynb?download=false 4/7
4/1/24, 4:05 PM filehandling
data store untill user press x and then data append to the file
project=open("project.txt","a")
while True:
name=input("Enter Your Name: ")
rollno=int(input("Enter Your Roll No: "))
semester=input("Enter Your current Semester: ")
Gpa=float(input("Enter your CGPa 4.0/? : "))
alldata=name+","+str(rollno)+","+semester+","+str(Gpa)+","
project.write(alldata)
project=open("project.txt","a")
while True:
project.write(alldata)
In [ ]:
CSV FILE
In [2]: import csv
Data added...
In [5]: # Create a function to add a contact: Define a function called add_contact() that prompts the user for contact informati
# Create a function to view contacts: Define a function called view_contacts() that reads the CSV file and displays all
import csv
def add_contact():
name=input("Name:")
email=input("Email:")
phone_number=input("Phone:")
with open("addressbook.csv","a",newline="") as book:
obj=csv.writer(book)
obj.writerow(["Name","Email","Phone"])
data=name,email,phone_number
obj.writerow(data)
book.close()
print("added")
add_contact()
def view_contacts():
with open("addressbook.csv","r") as book:
csvreader = csv.reader(book)
for row in csvreader:
print(row)
view_contacts()
Name:yasir
Email:ggg@tes.com
Phone:87867576858
added
['Name', 'Email', 'Phone']
['tariq', 'aca@gmail.com', '7897897']
['Name', 'Email', 'Phone']
['asnaf', 'test@123.com', '676757656745']
['Name', 'Email', 'Phone']
['yasir', 'ggg@tes.com', '87867576858']