Write a python program to handle data from/to files (open, read/write, delete to handle data). It uses exception handling – try and except to catch and handle exceptions.
import os def read_file( filename ): print("* We are in reading section "+filename) try: f = open(filename, "r") print(f.read()) f.close() except: print("* something is not working with read files") def write_file(filename): print("* We are in writing section "+filename) try: f = open(filename, "a") content = input("* Enter the content that you would like to add to the file:") f.write(content) f.close() except: print("* something is not working with write files") def delete_file(filename): print("* We are in deleting section "+filename) try: if os.path.exists(filename): os.remove(filename) print("file file is removed") else: print("file doesn't exist") except: print("something is not working with delete files") mode = int(input("* What would you like to do? Press 1 to Read, Press 2 to Write, Press 3 to Delete ")) print("* You chose the mode as : ", mode) filename = input("* Enter the filename: ") print("* You mentioned the filename as : "+filename) if (mode == 1): read_file(filename) elif (mode == 2): write_file(filename) elif (mode == 3): delete_file(filename) else: print("* this is not a valid option, please try again")
Raw Code:
import os
def read_file( filename ):
print("* We are in reading section "+filename)
try:
f = open(filename, "r")
print(f.read())
f.close()
except:
print("* something is not working with read files")
def write_file(filename):
print("* We are in writing section "+filename)
try:
f = open(filename, "a")
content = input("* Enter the content that you would like to add to the file:")
f.write(content)
f.close()
except:
print("* something is not working with write files")
def delete_file(filename):
print("* We are in deleting section "+filename)
try:
if os.path.exists(filename):
os.remove(filename)
print("file file is removed")
else:
print("file doesn't exist")
except:
print("something is not working with delete files")
mode = int(input("* What would you like to do? Press 1 to Read, Press 2 to Write, Press 3 to Delete "))
print("* You chose the mode as : ", mode)
filename = input("* Enter the filename: ")
print("* You mentioned the filename as : "+filename)
if (mode == 1):
read_file(filename)
elif (mode == 2):
write_file(filename)
elif (mode == 3):
delete_file(filename)
else:
print("* this is not a valid option, please try again")
Output


NOTE: If you like my work, please review with thumbsup. Write in comment in case of any issues
Write a python program to handle data from/to files (open, read/write, delete to handle data). It...
write a python program using sockets to read udp packets from a pcap files
Write a Python program that stores the data for each player on the team, and it also lets the manager set a starting lineup for each game. Questions: - Handle the exception that occurs if the program can’t find the data file. - Handle the exceptions that occur if the user enters a string where an integer is expected. - Handle the exception that occurs if the user enters zero for the number of at bats. - Thoroughly test the...
Using PYTHON create a program to allow the user to enter a phrase or read a file. Make your program menu-driven to allow the user to select a option to count the total words in the phrase or file. In addition, provide a menu-selection to count the number of words start with a vowel and a selection to count the number of words that start with a consonant. Include exception handlers for file not found, divide by zero, and index...
Chapter 08 Python Assignment: Question 1-5 Please I need help in my python course. Question 1 The finally clause of a try statement a. can be used to recover from an exception b. is required c. can be used to display more information about an exception d. is executed whether or not an exception has been thrown 10 points Question 2 Which of the following is the correct way to code a try statement that catches any type of exception...
3. Handling exceptions with a finally clause a. Download the following file from the class website: TestFinally.java b. Examine the code to determine what it does. c. Compile the code and notice the error. d. Modify TestFinally.java to handle the exception following these instructions (and those embedded in the code): i. Embedded in the code is a note showing the location for the try statement. ii. The first line of the catch block should look like this: catch(IOException e) {...
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()
.) Write a program to implement the exception handling with multiple (at least 3) catch statements. Any type of exception may be thrown. Catch statements must display (cout) the type of exception thrown. .) Write a function which accepts an index and returns the corresponding element from an array. If the index is out of bounds, the function should throw an exception. Handle this exception in "main()". (This is pretty open ended, so anything from a calculator or "what have...
Why to handle exceptions in a program? a. to protect data that are processed in a program b. to prevent a program from being stopped due to thrown exception c. to test and debug a program d. to fix errors
Write a program that prompts the user to enter a person's birth date in numeric form (e.g. 8-27-1980) and outputs the date of birth in the format of month day, year (e.g. August 27, 1980). Your program must work for three types of exceptions - invalid day, invalid month and invalid year (year must be between 1915 and 2015). Make these as class driven exceptions. Use one try-catch block with separate catches for the exception classes to handle errors for...
( Python Program) explain how Input/Output data files work in a Python program, and how you would use a loop to process input and/or output. Define the differences in a field, record and file, and define what an exception is.