Question: Read a String from a File Create a new text file in your prac _ 0 9 directory called name.txt . Type only your own

Read a String from a File
Create a new text file in your prac_09 directory called name.txt .
Type only your own name in the file.
Write a program in question_4() that reads this name.txt file and greets you, example:
Greetings Lindsay!
Note that since we want the entire contents of the file in one string, we don't need any kind of loop, we can just read
the file, like:
in_file = open("name.txt","r")
text = in_file. read()
Did it work?
If you got something like
Greetings Lindsay
!
Then what happened is the text variable includes the new line character ( In ) in your file.
You can solve this problem in two ways.
Edit the file so there's no new line
Strip the new line character (any whitespace from the ends of the line) from text with:
text = in_file.read ()*strip()
 Read a String from a File Create a new text file

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!