1

I have this

    f = open(os.path.join(r"/path/to/file/{}.txt").format(userid), "w")
    f.write(str(points))
    f.write(str(level))
    f.write(str(prevtime))
    f.close()

I know about using with open(blah) as f: and prefer this but when I have this code, even if I write the file first and then change to append mode, without adding a +"\n" it doesn't add to a new line. The reason \n is a problem is that when I go to get the data using

    f = open(os.path.join(r"blah\{}.txt").format(userid), "r")
    lines = f.readlines()
    points = float(lines[0])

I'll get an error telling me it can't interpret (for example: 500\n) as a float because it reads the \n. Any idea what I can do?

EDIT I ended up fixing it by just not making it a float, but now that is giving me a ValueError Unconverted Data Remains. These issues are only happening due to the line in the txt file that should contain a date in the format of %H:%M

EDIT 2019 I see lots of people trying to search the same question. My problem actually ended up with me ignoring and having a very clear lack of understanding of types in Python.
To answer the question that I imagine many people are searching for when they view this, \n is Python's newline character. An alternative (if using print()) would to be to call print() exactly as shown with no data, resulting in a blank line.

6
  • 2
    Why are you using os.path.join without joining anything? Commented Nov 1, 2016 at 22:56
  • Why not use rstrip to get rid of the \n Commented Nov 1, 2016 at 22:57
  • 5
    It looks like you have some bug you don't understand, and you've wrongly blamed the use of \n. \n is fine; you have some other issue. Commented Nov 1, 2016 at 22:57
  • Do you want \ns in the file, or not? Commented Nov 1, 2016 at 22:57
  • 3
    float("500\n") should not fail. Can you give us a complete, self-contained program that reproduces your error? Commented Nov 1, 2016 at 22:58

2 Answers 2

3

So, you have something like this

>>> f = open("test.txt", "w")
>>> f.write(str(4))
>>> f.write(str(20))
>>> f.close()
>>> f = open("test.txt")
>>> f.readlines()
['420']

But, you need to write newlines, so just do so

>>> f = open("test.txt", "w")
>>> f.write("{}\n".format(4))
>>> f.write("{}\n".format(20))
>>> f.close()
>>> f = open("test.txt")
>>> f.readlines()
['4\n', '20\n']
>>> f.close()

If you need no newline characters, try read().splitlines()

>>> f = open("test.txt")
>>> f.read().splitlines()
['4', '20']

EDIT

As far as the time value is concerned, here's an example.

>>> from datetime import datetime
>>> time_str = datetime.now().strftime("%H:%M")
>>> time_str
'18:26'
>>> datetime.strptime(time_str, "%H:%M")
datetime.datetime(1900, 1, 1, 18, 26)
Sign up to request clarification or add additional context in comments.

Comments

1

To print without newlines, use below. But without newlines, you might need to add some separator like space to separate your data

 >>> sys.stdout.write('hello world')
hello world>>>

With the newlines remain, you could use rstrip to strip off the newlines when reading out

lines = f.readlines()
points = float(lines[0].rstrip)

Alternatively, I prefer more pythonic way below

lines = f.read().splitlines()
points = float(lines[0])

1 Comment

Just learn that int or float function will works even the numeric string contains newline \n

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.