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
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...
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...
( 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.
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...
Write a PYTHON program that asks the user for the name of the file. The program should write the contents of this input file to an output file. In the output file, each line should be preceded with a line number followed by a colon. The output file will have the same name as the input filename, preceded by “ln” (for linenumbers). Be sure to use Try/except to catch all exceptions. For example, when prompted, if the user specifies “sampleprogram.py”...
CSC 252 Week 4 HW Provide code and full projects neatly and in proper form and in the correct header and cpp files. Code must compile, link and run for any credit.This is Microsoft visual Studio C++ and needs to be done correctly and neatly and I need to be able to copy and paste code to visual studio and this is for a grade so if you don't know the answer don't answer please and thank you. Exception Handling...