153k views
0 votes
Write a python program that allows the user to create, read, and append data to a file. The program should ask for the person's name, University; that they attend, major in school, and expected year of graduation. All information for each person should be placed on one line and separated by commas. The program must be able to read the information and format each individual in the file, placing each piece of information for the individual on a separate line, with a blank line between people.

User TysonSubba
by
8.2k points

1 Answer

7 votes

Here's a Python program that allows the user to create, read, and append data to a file as per your requirements:

```python

def create_file():

file_name = input("Enter the file name: ")

try:

with open(file_name, 'w') as file:

print("File created successfully.")

except:

print("An error occurred while creating the file.")

def add_person():

file_name = input("Enter the file name: ")

person_data = input("Enter person's information (name, university, major, graduation year): ")

try:

with open(file_name, 'a') as file:

file.write(person_data + '\\')

print("Person added successfully.")

except:

print("An error occurred while adding the person.")

def read_file():

file_name = input("Enter the file name: ")

try:

with open(file_name, 'r') as file:

print("File Contents:")

print(file.read())

except:

print("An error occurred while reading the file.")

def format_data():

file_name = input("Enter the file name: ")

try:

with open(file_name, 'r') as file:

lines = file.readlines()

formatted_data = ""

for line in lines:

data = line.strip().split(',')

formatted_data += "\\".join(data) + "\\\\"

print("Formatted Data:")

print(formatted_data)

except:

print("An error occurred while formatting the data.")

def main():

while True:

print("\\Menu:")

print("1. Create a file")

print("2. Add person's information to the file")

print("3. Read the file")

print("4. Format the data in the file")

print("5. Quit")

choice = input("Enter your choice (1-5): ")

if choice == "1":

create_file()

elif choice == "2":

add_person()

elif choice == "3":

read_file()

elif choice == "4":

format_data()

elif choice == "5":

print("Goodbye!")

break

else:

print("Invalid choice. Try again.")

if __name__ == '__main__':

main()

```

This program provides a menu to the user with the following options:

1. Create a file

2. Add person's information to the file

3. Read the file

4. Format the data in the file

5. Quit

You can choose the desired option by entering the corresponding number. The program uses the file name provided by the user to perform the specified operation on the file.

User Ginu Jacob
by
9.1k points