Home »
Python »
Python Programs
Choice-based read-write program in Python - Basic File Manager Project
Python File Manager Project: In this tutorial, we will write a Python program in which we will ask the user to choose options to read or write in files in python. This can be treated as a basic project that you can use to learn Python's File Management concepts.
By Shivang Yadav Last updated : July 05, 2023
Elementary Level Python File Manager Project
Problem 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.
Some articles to recap all the concepts required,
Problem solution
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.
Python program to create file manager project
Main.py file
# 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 file
# 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
Python file handling programs »