0

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()
EnlightenedFunky
  • 303
  • 1
  • 13
  • The answer is more or less given in your question: to write the txt '"\n" you type two characters '\' and 'n' (thus taken together called an escape sequence). A newline, or to be more precise, a linefeed (LF, 'advance down one line'), on other other hand is just one 'character', i.e. one byte (value decimal 10, or hex 0xA); it is usually represented in texts, such as your question, by the two typeable characters '\' and 'n'. (It depends on the interpreter (like python) or compiler (e.g. for a C-source file) on how to 'translate' that escape-sequence.) –  Jun 16 '19 at 02:10
  • @Roadowl So if I typed \n\ would that work? because I was reading in some posts about that... – EnlightenedFunky Jun 16 '19 at 02:12
  • Work to do what? –  Jun 16 '19 at 02:14
  • Here's an explanation: https://stackoverflow.com/questions/54410812/how-do-you-input-escape-sequences-in-python – Agile Jun 16 '19 at 02:25
  • Not an answer, but can I suggest using \r\n just to keep things neat? – MegaEmailman Jun 16 '19 at 06:11

3 Answers3

1

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()
Xavi Martínez
  • 2,125
  • 1
  • 16
  • 17
1

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()
MegaEmailman
  • 505
  • 3
  • 11
1

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()
Agile
  • 66
  • 2