0

I have code that converts an integer into a string to write it into a file, but the code to convert it back into an integer so I can put it through simple equations doesn’t work.

my code: writing:

saveposx = open("sevedposx.txt", "w")
saveposy = open("sevedposy.txt", "w")
saveposx.write(str(x))
saveposy.write(str(y))

where x = 32 and y = 32 as well

reading:

readposx = open("sevedposx.txt", "r")
readposy = open("sevedposy.txt", "r")
posx = readposx.read(50)
posy = readposy.read(50)
actposx = int(posx)
actposy = int(posy)
actpos = ((actposx - 2), (actposy - 2))

x and y are defined outside a loop as to allow rewriting of x and y.

My code gives me this error:

  File "name_yet_to_come.py", line 399, in <module>
    actposx = int(posx)
ValueError: invalid literal for int() with base 10: ''

Thanks

Tripp Kinetics
  • 5,178
  • 2
  • 23
  • 37
Sudoadmin
  • 35
  • 2
  • 8
  • 1
    Did you ever close `saveposx` and `saveposy`? You need to do so to commit the changes to the file. –  Jul 02 '14 at 16:05
  • 1
    This could be because of any number of different problems, so here is some debugging advice: Check whether the files `sevedposx.txt` and `sevedposy.txt` actually exist and actually contain what you expect them to. Insert `print(repr(posx))` immediately after `posx=readposx.read(50)` and similarly for `posy`; do you get what you expect? – zwol Jul 02 '14 at 16:06
  • they are both present and contain 32 in both, and they print correctly – Sudoadmin Jul 02 '14 at 16:10
  • Do you mean, that adding `print "DEBUG:", posx` right before `actposx = int(posx)` prints `DEBUG: 32` and then raises the exception? – bereal Jul 02 '14 at 16:16
  • no it just prints DEBUG: 32 in the python interpreter, when I wrote it into the program it prints and then returns the error – Sudoadmin Jul 02 '14 at 16:19
  • Print the `repr()`: `print "posx:", repr(posx)`, and tell us the exact output, no paraphrasing. – Kijewski Jul 02 '14 at 16:22

2 Answers2

0

The error message indicates that the file is empty. Probably the output file variable is still referenced / in the scope, or was not garbage collected yet.

Use

with open(...) as f:
    pass # do something with f here

to have the file automatically flushed and closed at the end of the scope.

Or just close them explicitly with f.close(), but consider using a try finally block then.

You can open multiple files in this scope, too, as further explained in How can I open multiple files using "with open" in Python?.

with open('a', 'w') as a, open('b', 'w') as b:
    pass # do something with a and b here

If you want to do IPC, then use a TCP stream or a Pipe() (or better yet: Queue()) instead.

Community
  • 1
  • 1
Kijewski
  • 25,517
  • 12
  • 101
  • 143
  • they are both written to the file and read properly, I ran the python interpreter and told it r = open("sevedposx.txt", "r") and print r.read(2) and it returns 32 as it should. – Sudoadmin Jul 02 '14 at 16:15
  • That does not make sense. After the program finished, the files are of cause present and fully written. The problem occurs only during the run of the program. Just print out the repr() of posx/y before the integer conversion. – Kijewski Jul 02 '14 at 16:16
-1

You have to close the file after writing to make sure that everything is saved. So for the writing part you should add the lines

saveposx.close()
saveposy.close()
jfschaefer
  • 54
  • 2
  • 3
  • after I add that it returns ValueError: invalid literal for int() with base 10: '31.97' which is different from before only because it adds '31.97' – Sudoadmin Jul 02 '14 at 16:18
  • 31.97 seems to be almost 32. Are you sure that x and y are of type int? You could try to use saveposx.write(str(int(round(x)))) – jfschaefer Jul 02 '14 at 16:23
  • when I wrote it to the file I had to convert it to str, in the files they both say 32, so no they are not an int when in the file, when I did that it worked @user336957 – Sudoadmin Jul 02 '14 at 16:27