0

I know there are a lot of questions about file.read() but I dont think any of them answer this.... Can someone tell me why, if I read a file with say 12 lines, in the interpreter and print it by just typing in filename.read(), it is presented on one continuous line with the EOL character to signify each line?

Then, if I do the same but create define an object, say file-output = filename.read(), and then print that object, it then prints out as it appears in the file?

Does what I said make sense????

Many thanks

kaj0103
  • 1
  • 1
  • 1
    If you type just `filename.read()` the IDE will show you the representation of the string. If you do `file_output = filename.read()` and then `print(repr(file_output))` you'll see the same result. – Matthias Nov 29 '18 at 17:32
  • 2
    I suspect this doesn't have anything to do with file objects. If you write `"a\nb"` in your REPL, it will print `'a\nb'`. If you do `print("a\nb")`, it will write `a`, and then `b` on a new line. That's just how strings work. – Kevin Nov 29 '18 at 17:32
  • unable to repro - this might be related to "where" you print it - python console maybe? I just used `with open("t.txt","w") as f: f.write("1\n2\n3\n4")` and `with open("t.txt") as f: print(f.read())` inside https://pyfiddle.io/ - and it prints normally for 2.7and 3.6 – Patrick Artner Nov 29 '18 at 17:33
  • Thanks all... that makes sense.. – kaj0103 Dec 02 '18 at 15:41

0 Answers0