Python Forum

Full Version: new line character
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I've got a simple code which should open, read, write, and append code. On the 3rd block of code, it returns the text from the 2nd block on the same line as the 3rd. How do I get the appended text to print on the line below it in the txt file? I'm just looknig for a carriage return (I think), but I tried in the 3rd block using this line, but it didn't do what I wanted:

file.write("[b]\r[/b]This is the *Appended* text written to the file.")


Code 1
file = open("demo.txt", 'r') # (which file to open, mode)  Mode means what you're doing to the file (e.g. write or read)
                              #'w' = write to file, 'r' = read file

#read file which you've opened
content = file.read() #after reading file, it must safe the content, e.g. in a variable, 'content'
#content = file.readline()  # this will only read the first line of the txt
#content = file.read(10)     # this will read only the first 10 bytes (characters) of the code
print(content)   # print what was read

# after opening a file, you always have to close it
file.close()
Code 2 - previous text in demo.txt file will be overwritten
#opens a file, and tells pgrogram that code will write to it
file = open("demo.txt", "w")
file.write("This is the *NEW* text written to the file.")  #writes text to file
file.close()  #closes file

file = open("demo.txt", "r")  #opens file, tells program it will write to it
content = file.read()         #reads file, returns info to var 'content'
print(content)                #prints var content
file.close()                  #closes file
Code 3 - previous text in demo.txt file will appended, so it won't get text deleted
#opens a file, and tells pgrogram that code will write to it
file = open("demo.txt", "a")
file.write("This is the *Appended* text written to the file.")  #writes text to file, won't delete previous
file.close()  #closes file

file = open("demo.txt", "r")  #opens file, tells program it will write to it
content = file.read()         #reads file, returns info to var 'content'
print(content)                #prints var content
file.close()                  #closes file
When you print() data, by default a newline is added at the end. When you write() data, there is no automatic addition of newlines. The data in the two write() calls has no newline, so there is nothing to separate them.

It would be suggested to append newlines to your write() strings if they're supposed to be lines.

Either:
file.write("This is the *NEW* text written to the file.\n")  #writes text to file
or

file.write("This is the *NEW* text written to the file.")  #writes text to file
file.write("\n")
or some variant thereof.
I would suggest to open files using with.

In addition to bowlofred answer: you can also print to the file. It is useful when newlines needed to be added at the end. It's not useful when line already contains newline at the end - this results in two newlines.

print("This is the *NEW* text written to the file.", file=file)
Documentation is available either from interactive interpreter:
>>> help(print)
Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
or on Python website: print()
Great, thank you everyone! I was able to get my project to work with the simple addition of file.write("\n")