Q:

Choice-based read-write program in Python - Basic File Manager Project

belongs to collection: Python file handling programs

0

Elementary Level Python File Manager Project

Statement: Provide user an option to choose from three options:

  • Read data from file
  • Write data to a file
  • Exit the program

Based on the choice, the user can read data from a file, he/she wants to. Write data to the file or exit from the program.

Get inputs on filename and data from the user. And try to make the program modular i.e. make separate files for utility functions.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

File: file.txt (after)

hello! python programmers 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to read character till a count... >>
<< Python program to delete a file...