To create a simple choice based program, you need to use a conditional statement, we have used if-else for you to easily understand. You can use others too.
For choice 1, read file, We will ask the user for the file name and print its contents.
For choice 2, write file, We will ask the user for data and file name, and then write content to the file, if the file is not present write will create a new file
For choice 3, exit, Exit the program.
**Try to create the program yourself first and then refer to this code, your program can be more efficient than this one, as we have written this program so that everyone can easily understand.
Program for choice-based read-write in Python
Main.py
# Importing library and functions we have created
from os import system
from FileManager import *
def main():
# Choice for options for user to select
ans=True
while(ans):
system('cls')
print("File Menu")
print("1. Write Data to a file. ")
print("2. Read Data from a File. ")
print("3. Exit")
ch=int(input("Enter Choice(1-3) : "))
if ch==1:
data = input("Enter Data : ")
fileName = input("Enter File name : ")
# calling the function from FileManager.py
WriteToFile(data,fileName)
print("Data is Saved.")
elif ch==2:
fileName = input("Enter File name : ")
# calling the function from FileManager.py
data = ReadFromFile(fileName)
print("Content of File:\n",data)
elif ch==3:
ans=False
else:
print("Invalid Choice")
input("\n\nPress any key .........")
if __name__=="__main__":main()
FileManager.py
# Function to write data to a file
def WriteToFile(data,filename):
fileobj=open(filename,"w")
fileobj.write(data)
fileobj.close()
# Function to read data from a file
def ReadFromFile(filename):
fileobj=open(filename,"r")
data=fileobj.read()
fileobj.close()
return data
Output:
File Menu
1. Write Data to a file.
2. Read Data from a File.
3. Exit
Enter Choice(1-3) : 2
Enter File name : file.txt
Content of File:
This is a demo file.
Learn python programming at includehelp.com
Press any key .........
File Menu
1. Write Data to a file.
2. Read Data from a File.
3. Exit
Enter Choice(1-3) : 1
Enter Data : hw ello! python programmers
Enter File name : file.txt
Data is Saved.
Press any key .........
File Menu
1. Write Data to a file.
2. Read Data from a File.
3. Exit
Enter Choice(1-3) : 2
Enter File name : file.txt
Content of File:
hello! python programmers
Press any key .........
File Menu
1. Write Data to a file.
2. Read Data from a File.
3. Exit
Enter Choice(1-3) : 3
Press any key .........
File: file.txt (before)
This is a demo file.
Learn python programming at includehelp.com
To create a simple choice based program, you need to use a conditional statement, we have used if-else for you to easily understand. You can use others too.
We will ask the user for the file name and print its contents.
We will ask the user for data and file name, and then write content to the file, if the file is not present write will create a new file
Exit the program.
**Try to create the program yourself first and then refer to this code, your program can be more efficient than this one, as we have written this program so that everyone can easily understand.
Program for choice-based read-write in Python
Main.py
FileManager.py
Output:
File: file.txt (before)
File: file.txt (after)
need an explanation for this answer? contact us directly to get an explanation for this answer