ToxicLiquidz101 ToxicLiquidz101 - 1 year ago 86
Python Question

I keep getting a read issue [Errno 22] Invalid argument

I have tried putting the

(r"F:\Server\ ... "r")
it says:

file.write(float(u) + '\n')
TypeError: unsupported operand type(s) for +: 'float' and 'str'.

When I don't put the
were it is it will double \\ on me saying:

read issue [Errno 22] Invalid argument: 'F:\\Server\\Frames\\Server_Stats_GUI\x08yteS-F_FS_Input.toff'.

Here is my code

import time

while True:
file = open("F:\Server\Frames\Server_Stats_GUI\byteS-F_FS_Input.toff","r")
f = int(file.readline())
s = int(file.readline())
except Exception as e:
# file is been written to, not enough data, whatever: ignore (but print a message)
print("read issue "+str(e))
u = s - f
file = open("F:\Server\Frames\Server_Stats_GUI\bytesS-F_FS_Output","w") # update the file with the new result
file.write(float(u) + '\n')
time.sleep(4) # wait 4 seconds

Answer Source

You have two separate errors here.

1: Filename with Escape Characters

This error:

read issue [Errno 22] Invalid argument: 'F:\Server\Frames\Server_Stats_GUI\x08yteS-F_FS_Input.toff'.

Is in the open() function.

Your filename has an escape character in it. '\b' is being evaluated to '\x08' (backspace). The file is not found, which throws an error.

To ignore escape characters, you can either double the backslash:


or use r as a prefix to the string:


You've tried the second way, which fixed that issue.

2: TypeError on Write()

The next error:

file.write(float(u) + '\n') TypeError: unsupported operand type(s) for +: 'float' and 'str'.

is in the write() function.

You're treating a float as a string. You need to convert it to a string before trying to append the newline:

file.write(str(float(u)) + '\n')

or use string formatting:

file.write("%f\n" % (float(u)))