-1

I am learning Python and using Jupyter to write Python code. I am working on File Handling currently.

My Code goes like this:

file = open(r"C:\USERS\PRAGA\DESKTOP\TestFile.txt",'w')
file.write("I can do it")
file.close()
file = open(r"C:\USERS\PRAGA\DESKTOP\TestFile.txt",'a')
file.write("\nBelieve in yourself. Yes, you can do it.")
file = open(r"C:\USERS\PRAGA\DESKTOP\TestFile.txt",'r')
Last_content = file.read()
Last_content

The final answer is

I can do it\nBelieve in yourself. Yes, you can do it.

Expected answer:

I can do it
Believe in yourself. Yes, you can do it.

When I opened the text file, the expected answer is coming but but not getting displayed in the editor.

Why the newline character is not working?

Pragati
  • 7
  • 1

1 Answers1

0

EDIT:

Look at the following example:

enter image description here

Clearly, we get the desired output.
However, in your code you simply do Last_content in the last line, which redirects the content to the notebook, which eventually displays it as a text string. When jupyter handles the text to display it on screen, it escapes special characters to avoid errors in framework. Hence, you get your problem. (Pointed out by @SergiyKolesnikov, it is done by internal representation)

EDIT2: Plase see complete screenshot:

enter image description here

mr_mo
  • 1,401
  • 6
  • 10