Question

Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() -...

Write a program that performs the following:
- Reads from the file "myfile.txt" using readlines()
- Prints out the contents of the file "myfile.txt", that was read into a list by readlines(). Note that this should not look like a list, so you will need to loop through the list created by readlines and print the text.
- Use the try/except method to create the file if it does not exist
- If the file does not exist, prompt the user in a loop to enter a line of text. End the loop when the user types "end"
- Save the text to a file called "myfile.txt" using write()
- Don't forget to close the file.

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

#when file not exist

#running second time

code:

def main():

#read file in try catch mode

try:

#open file in read mode

read_file = open("myfile.txt","r")

#read lines as list

lines = read_file.readlines()

#print each line content

for line in lines:

print(line)

#close the file

read_file.close()

except FileNotFoundError:

#if file not exist

#create a file in write mode

write_file = open("myfile.txt","w")

#ask user to enter the lines

while True:

line = input("Enter line : ")

#check if user enters end

if line.lower() == "end":

break

#write line into file with a new line

write_file.write(line+"\n")

#close the file

write_file.close()

main()

Add a comment
Know the answer?
Add Answer to:
Write a program that performs the following: - Reads from the file "myfile.txt" using readlines() -...
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
  • 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...

  • //Done in C please! //Any help appreciated! Write two programs to write and read from file...

    //Done in C please! //Any help appreciated! Write two programs to write and read from file the age and first and last names of people. The programs should work as follows: 1. The first program reads strings containing first and last names and saves them in a text file (this should be done with the fprintf function). The program should take the name of the file as a command line argument. The loop ends when the user enters 0, the...

  • Following should be done in C++: Create a program that reads a text file and prints...

    Following should be done in C++: Create a program that reads a text file and prints out the contents of the file but with all letters converted to uppercase. The name of the file to be read by the program will be provided by the user. Here are some example session: Contents of "data.txt": Hello, World! User input: data.txt Program output: HELLO, WORLD! Contents of "data.txt": tHiS FiLe HaS mIxEd CaSeS User input: data.txt Program output: THIS FILE HAS MIXED...

  • Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method.

     Write a program that first reads in the name of an input file and then reads the file using the csv.reader() method. The file contains a list of words separated by commas. Your program should output the words and their frequencies (the number of times each word appears in the file) without any duplicates. Ex: If the input is: inputl.csv and the contents of input1.csv are: hello, cat, man, hey, dog, boy, Hello, man, cat, woman, dog, Cat, hey, boy the output is: hello 1 cat 2 man...

  • (Python 3) Write a program that reads the contents of a text file. The program should...

    (Python 3) Write a program that reads the contents of a text file. The program should then create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears and a list that contains the line numbers in the file where the word (the key) is found. Then the program will create another text file. The file should contain an alphabetical listing of the words that are...

  • Write a program that performs the following: 1. Presents the user a menu where they choose...

    Write a program that performs the following: 1. Presents the user a menu where they choose between:              a. Add a new student to the class                           i. Prompts for first name, last name                           ii. If assignments already exist, ask user for new student’s scores to assignments              b. Assign grades for a new assignment                           i. If students already exist, prompt user with student name, ask them for score                           ii. Students created after assignment will need to...

  • Question 7 (a) Write a C program that performs the following tasks: 1. Create a text...

    Question 7 (a) Write a C program that performs the following tasks: 1. Create a text file to store a list of numbers. 2. Read a number entered by a user and store it into the text file until a negative number is entered. (b) Write a C program that performs the following tasks: 1. Open the text file that contains a list of numbers. 2. Print the list of numbers stored in the text file on the screen.

  • in c++ please. Write a program that reads the contents of a text file. The program...

    in c++ please. Write a program that reads the contents of a text file. The program should create a map in which the keys are the individual words found in the file and the values are the number of times each word appears. For example, if the word "the" appears 128 times, the map would contain an element with "the" as the key and 128 as the value. The program should either display the frequency of each word or create...

  • In C write a text analyzer program that reads any text file. The program displays a...

    In C write a text analyzer program that reads any text file. The program displays a menu that gives the user the options of counting: • Lines • Words • Characters • Sentences (one or more word ending with a period) • or all of the above Provide a separate function for each option. At the end of the analysis, write an appropriate report.

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

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