EnlightenedFunky
EnlightenedFunky

Reputation: 325

Text Files, and New Lines

If I have this code to write a txt file I am just wondering why every time I type \n in the input it doesn't create a newline so like this.

This is the example txt file\n it doesn't create a new line\n how can I make it do it? 

This is the code

def create():
     path = input("What file would you like to create: ")
     output_file = open(path, 'x')
     text = input("What would you like to write? ")
     output_file.write(text)
     output_file.close()
def append():
     path = input("What file would you like to append: ")
     output_file = open(path, 'a')
     text = input("What would you like to write?")
     output_file.writelines(["\n", text])
     output_file.close()
write = input("Would you like to write something today, or edit? ")
if write == "Write":
        create()
if write == "Edit":
        append()

Upvotes: 0

Views: 1179

Answers (3)

Agile
Agile

Reputation: 66

The reason why \n doesn't work with input is mentioned in How do you input escape sequences in Python?. The input function doesn't recognize any escape sequences, such as \n, because it interprets the text literally.

To interpret \n as a new line from text, have these imports and adjust the arguments for write() and writelines() allowing a user to input, This is the example txt file\n it doesn't create a new line\n how can I make it do it? to include new lines:

import ast # Add imports
import shlex

def create():
    path = input("What file would you like to create: ")
    output_file = open(path, 'x')
    text = input("What would you like to write? ")
    output_file.write(ast.literal_eval(shlex.quote(text))) # Evaluate escape sequences
    output_file.close()


def append():
    path = input("What file would you like to append: ")
    output_file = open(path, 'a')
    text = input("What would you like to write?")
    output_file.writelines(["\n", ast.literal_eval(shlex.quote(text))]) # Evaluate escape sequences
    output_file.close()

write = input("Would you like to write something today, or edit? ")
if write == "Write":
    create()
if write == "Edit":
    append()

Upvotes: 1

MegaEmailman
MegaEmailman

Reputation: 545

Here's a very simplified answer.

input = input("Would you like to Write a new document, or Edit an existing one?  ")
if input == Write:
    file = open(path, "w")
    file.write(text)
    file.write("\r\n")
    file.close()
if input == Edit:
    file = open(path, "a")
    file.write(text)
    file.write("\r\n")
    file.close()

Upvotes: 1

Xavi Martínez
Xavi Martínez

Reputation: 2161

You can use replace:

def create():
    path = input("What file would you like to create: ")
    output_file = open(path, 'x')
    text = input("What would you like to write? ")
    output_file.write(text.replace('\\n', '\n'))
    output_file.close()
def append():
    path = input("What file would you like to append: ")
    output_file = open(path, 'a')
    text = input("What would you like to write?")
    output_file.writelines(text.replace('\\n', '\n'))
    output_file.close()
write = input("Would you like to write something today, or edit? ")
if write == "Write":
    create()
if write == "Edit":
    append()

Upvotes: 1

Related Questions