Skip to content

Feat/file reader #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.formatting.provider": "none"
}
22 changes: 22 additions & 0 deletions F/file-reader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
### File Reader

This is a simple Python program that reads and displays the content of a text file. It allows you to specify the file name, and it will display the contents of that file. The program handles common error cases more gracefully.

#### Usage

1. Run the program using a Python interpreter.
2. You will be prompted to enter the name of the file you want to read.
3. The program will then attempt to read and display the content of the specified file.
4. If the file is found and can be read, its content will be displayed.
5. If the file is not found, an error message will be shown.
6. If any other error occurs during file reading, an error message will be displayed.

#### Program Features

- Graceful handling of errors:
- If the specified file is not found, it will display a user-friendly error message.
- If any other error occurs during file reading, it will display an error message with details.

#### Prerequisites

- Python 3.x
11 changes: 11 additions & 0 deletions F/file-reader/file-reader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
filename = input("Enter the name of the file to read: ")

try:
with open(filename, 'r') as file:
content = file.read()
print("File content:")
print(content)
except FileNotFoundError:
print(f"File '{filename}' not found.")
except Exception as e:
print(f"An error occurred: {e}")