177k views
16 votes
can someone help me with this code please. here it is: Add a command to this chapter’s case study program that allows the user to view the contents of a file in the current working directory. When the command is selected, the program should display a list of filenames and a prompt for the name of the file to be viewed. Be sure to include error recovery.

2 Answers

7 votes

Final Answer:

To add a command allowing the user to view file contents in the current working directory, implement code that displays a list of filenames and prompts the user to input the file name they wish to view. Ensure error handling for incorrect inputs or non-existent files.

Step-by-step explanation:

To fulfill this request, you'll need to augment the existing code with a functionality that lists the filenames in the current directory and prompts the user for the desired file to view. Start by using a method to list the files in the directory and present them to the user. Then, accept user input for the file to be viewed. Ensure error handling by verifying if the input corresponds to an existing file and handle potential exceptions, such as file not found or invalid inputs. Employ conditional statements to execute the viewing action based on valid inputs.

The process involves utilizing Python's `os` module to interact with the file system. Implement a function that retrieves file names in the current directory using `os.listdir()` and presents them as options. Employ user input to select a file for viewing. Use `try-except` blocks to manage potential errors, like incorrect inputs or non-existent files. Finally, open the selected file in read mode and display its contents for the user.

Consider employing loops for user-friendly interaction, ensuring the program continuously functions until the user exits or performs another action. This way, the program provides a seamless experience for file viewing within the current directory while maintaining robust error handling for various scenarios, enhancing user experience and program reliability.

User AmitM
by
5.0k points
11 votes

Final answer:

To view file contents in the current working directory, list available files using os.listdir(), prompt user input for the file name, and then read and display the file contents inside a try-except block for error recovery.

Step-by-step explanation:

To add a command that allows a user to view the contents of a file in the current working directory, you can follow these steps:

  1. Display a list of filenames using a command like os.listdir().
  2. Prompt the user for the name of the file they wish to view.
  3. Open and read the file's contents using a try-except block for error recovery.
  4. Print the contents of the file or an error message if the file cannot be found or opened.

Here is a sample code snippet in Python that incorporates the above steps:
import os
def view_file_in_directory():
# List all files in the current directory
files = os.listdir('.')
print('Files available to view:')
for f in files:
print(f)

# Prompt the user for the file name
file_name = input('Enter the name of the file to view: ')

try:
# Attempt to open and read the file
with open(file_name, 'r') as file:
print(file.read())
except FileNotFoundError:
print('That file does not exist. Please try again.')
except Exception as e:
print(f'An error occurred: {e}')

# Call the function
view_file_in_directory()

The script above lists the available files and asks the user for the file they wish to view. It then attempts to open and display the file content, handling any errors that may occur.

User Jacopo Lanzoni
by
4.6k points