# Community Practice Problem (File Handling)

write a program to take input of basic details such as name, contact_info, address, education from the user and store it in a
dictionary where the keys will be the fields ( name , contact_info , address, education) and values will be the data given by
user. now make a file for permanently storing details of the user.

2 Likes

def get_user_details():
details = {}
details[‘name’] = input("Enter your name: ")
details[‘contact_info’] = input("Enter your contact information: ")
details[‘address’] = input("Enter your address: ")
details[‘education’] = input("Enter your education: ")
return details

def save_details_to_file(details, filename):
with open(filename, ‘w’) as file:
for key, value in details.items():
file.write(f"{key}: {value}\n")

user_details = get_user_details()
filename = ‘user_details.txt’
save_details_to_file(user_details, filename)
print(f"User details have been saved to {filename} file.")

1 Like

Very well done, keep shining .

dict_1={
“name” : str(input(“Enter the name:”)),
“contact_info”:int(input(“Enter the mobile no:”)),
“address”:str(input(“Enter the address:”)),
“Qualification”:str(input(“Education Pursued:”))
}
print(dict_1)
file=open(“basic details.txt”,‘w’)
file.write(str(dict_1))
file.close()

def basic_details():
dic = {}
dic[‘name’] = input("Enter your name: ")
dic[‘contact_info’] = int(input("Enter your contact info: "))
dic[‘address’] = input("Enter your address: ")
dic[‘education’] = input("Enter your education: ")
return dic

user_detail = str(basic_details())
filename = open(‘user_detail.txt’,‘w’)
filename.write(user_detail)
filename.close()

1 Like

name = input(“name:”)
contact_info = input(“contact:”)
address = input(“address:”)
education = input(“education”)
details ={“name”: name,“contact_info”: contact_info,“address”:address,“education”:education}
file_name = “practice.txt”
file_name = open(“practice.txt”,“w+”)
file_name.write(str(details))
file_name.close()

1 Like

def get_user_details():
details = {}
details[‘name’] = input("Enter your name: ")
details[‘contact_info’] = input("Enter your contact information: ")
details[‘address’] = input("Enter your address: ")
details[‘education’] = input("Enter your education: ")
return details

def save_details_to_file(details):
file_name = “user_details.txt”
with open(file_name, ‘w’) as file:
for field, value in details.items():
file.write(f"{field}: {value}\n")
print(f"Details saved to {file_name}")

user_details = get_user_details()
save_details_to_file(user_details)

1 Like