Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
new line character
#1
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
Larz60+ write Jan-01-2021, 03:25 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use code tags on future posts.
Reply
#2
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.
Reply
#3
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()
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
Great, thank you everyone! I was able to get my project to work with the simple addition of file.write("\n")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,367 Sep-27-2022, 01:38 PM
Last Post: buran
  How can I add an end line character after every value that I receive? GiggsB 11 5,156 Mar-06-2022, 08:51 PM
Last Post: GiggsB
  [solved] unexpected character after line continuation character paul18fr 4 3,389 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,162 Jul-13-2020, 07:05 PM
Last Post: snippsat
  Regex won't replace character with line break Tomf96 2 2,547 Jan-12-2020, 12:14 PM
Last Post: Tomf96
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,732 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Replace changing string including uppercase character with lowercase character silfer 11 6,176 Mar-25-2019, 12:54 PM
Last Post: silfer
  SyntaxError: unexpected character after line continuation character Saka 2 18,547 Sep-26-2017, 09:34 AM
Last Post: Saka
  How do i get emacs to show a 79 character line limit ? Skaperen 7 9,005 Oct-17-2016, 08:48 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020