From 47bf10f57e40323bc14672e97afa53eaf3440abd Mon Sep 17 00:00:00 2001 From: Prashidika Tiwari Date: Sun, 22 Oct 2023 11:08:52 +0545 Subject: [PATCH 1/2] File Reader Program --- .vscode/settings.json | 6 ++++++ F/file-reader/README.md | 0 F/file-reader/file-reader.py | 11 +++++++++++ 3 files changed, 17 insertions(+) create mode 100644 .vscode/settings.json create mode 100644 F/file-reader/README.md create mode 100644 F/file-reader/file-reader.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..5c80254d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "[python]": { + "editor.defaultFormatter": "ms-python.autopep8" + }, + "python.formatting.provider": "none" +} diff --git a/F/file-reader/README.md b/F/file-reader/README.md new file mode 100644 index 00000000..e69de29b diff --git a/F/file-reader/file-reader.py b/F/file-reader/file-reader.py new file mode 100644 index 00000000..9e10a4f3 --- /dev/null +++ b/F/file-reader/file-reader.py @@ -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}") From 0087ad86cb35d9270b15f2fc89bc61c9d0b61f1d Mon Sep 17 00:00:00 2001 From: Prashidika Tiwari Date: Sun, 22 Oct 2023 11:09:23 +0545 Subject: [PATCH 2/2] READ ME file --- F/file-reader/README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/F/file-reader/README.md b/F/file-reader/README.md index e69de29b..86eacded 100644 --- a/F/file-reader/README.md +++ b/F/file-reader/README.md @@ -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