-1

heres the code

doc = open(path, 'w+')
lines = []
i = 0
for line in doc:
    lines.append(line)
    i += 1
    if i == 1:
        money = int(line)
print(lines)

heres what is returns

[]

the text file contains one line that says "1000"

I tried to resolve the issue by opening it with r, close it, and open it again with w. But for some reason it just deletes all of the text in the file when I try to write anything.

1 Answers1

2

opening it with w+ will overwrite the existing file with a new empty file (which you can then both write and read from) ...

you probably want to open it with a+ (to append) this will put the pointer at the end of the file and then to read before that you will need to use open_file.seek(0) (to go to the beginning of the file)

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179