Question

Please use python 3!!!! # Write a function "get_file" to ask the user for a file...

Please use python 3!!!!

# Write a function "get_file" to ask the user for a file name.
# Return an open file handle to the users file in "read" mode.
# Use exception handling to deal with the case that the user's file
# does not exist, printing an error saying "File does not exist, try again"
# and trying again

f = get_file()
f.close()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#!/usr/bin/python

def get_file():

#Reads the name of the file.

fileName = raw_input('Enter the name of the file to read: ')

try:

#Tries to read the file in read mode, and returns the handle.

fileHandle = open(fileName, 'r')

return fileHandle

except:

#Prints an error message, and retries, if fails to open the file.

print'File does not exist, try again.'

get_file()

f = get_file()

print f.read()

f.close()

Add a comment
Know the answer?
Add Answer to:
Please use python 3!!!! # Write a function "get_file" to ask the user for a file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Starting out with python 4th edition Write program that lets the user enter in a file...

    Starting out with python 4th edition Write program that lets the user enter in a file name (numbers.txt) to read, keeps a running total of how many numbers are in the file, calculates and prints the average of all the numbers in the file. This must use a while loop that ends when end of file is reached. This program should include FileNotFoundError and ValueError exception handling. Sample output: Enter file name: numbers.txt There were 20 numbers in the file....

  • Write a program to read a text file, place each line it reads into an array....

    Write a program to read a text file, place each line it reads into an array. Then print the array’s contents one line at a time. Then add to the end of the text file “Success”. Use error exception handling and if the text file does not exist print "Error file not found." Always print "It worked." as part of the try clause. Upload a zip folder file of the .py and text file. roses are roses are red, violets...

  • Ask the user for the name of a file. Read a list of numbers of unknown...

    Ask the user for the name of a file. Read a list of numbers of unknown length from the file. Find and print the MEDIAN and the MODE(S) of the set of numbers. Do not use python statistics functions to find the medium or mode To find the MODE(S) Create a dictionary using the numbers as the keys, and the values are how often that number appears in the list. For instance, with this list [1,1,5], the dictionary would be...

  • (IN PYTHON) Download file homeruns.txt from Canvas. Your program must ask the user for the file...

    (IN PYTHON) Download file homeruns.txt from Canvas. Your program must ask the user for the file name the read that file. Your program must catch an exception if the file isn’t found. It is a comma-delimited file with 2 fields on each line, a year and a number of home runs. Your program should have functions to determine the year with the most home runs, the year with the fewest number of home runs, and the average home runs for...

  • Could anyone please help with this Python code assignment? In this programming assignment you are to...

    Could anyone please help with this Python code assignment? In this programming assignment you are to create a program called numstat.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The count of how many numbers are in the file. The average of the numbers. The average is the sum of the numbers divided by how many there are. The maximum value. The...

  • In python Count the frequency of each word in a text file. Let the user choose...

    In python Count the frequency of each word in a text file. Let the user choose a filename to read. 1. The program will count the frequency with which each word appears in the text. 2. Words which are the spelled the same but differ by case will be combined. 3. Punctuation should be removed 4. If the file does not exist, use a ‘try-execption’ block to handle the error 5. Output will list the words alphabetically, with the word...

  • write a python program that prompts the user for a name of a text file, opens...

    write a python program that prompts the user for a name of a text file, opens that file for reading , and tracks the unique words in a file and counts how many times they occur in the file. Your program should output the unique words and how often they occur in alphabetical order. Negative testing for a file that does not exist and an empty file should be implemented.

  • Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as t...

    Function / File Read (USING MATLAB) A) create a user-defined function : [maxsample , maxvalue , numsamples]=writetofile(sname,strgth) to do the following: The user-defined function shall: -Accept as the input a provided string for the sample name (sname) and number for strength (strgth) - open a text file named mytensiledata.txt, with permission to read or append the file. - Make sure to capture any error in opening the file - Write sname and strgth to the file - Close the file...

  • Problem 1 Recall that when the built-in function open() is called to open a file for...

    Problem 1 Recall that when the built-in function open() is called to open a file for reading, but it doesn't exist, an exception is raised. However, if the file exists, a reference to the opened file object is returned. Write a function safeOpen() that takes one parameter, filename - a string giving the pathname of the file to be opened for reading. When safeOpen() is used to open a file, a reference to the opened file object should be returned...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT