ToxicLiquidz101 ToxicLiquidz101 - 4 months ago 8
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 are 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:
try:
file = open("F:\Server\Frames\Server_Stats_GUI\byteS-F_FS_Input.toff","r")
f = int(file.readline())
s = int(file.readline())
file.close()
except Exception as e:
# file is been written to, not enough data, whatever: ignore (but print a message)
print("read issue "+str(e))
else:
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')
file.close()
time.sleep(4) # wait 4 seconds

Answer

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:

"F:\Server\Frames\Server_Stats_GUI\\byteS-F_FS_Input.toff"

or use r as a prefix to the string:

r"F:\Server\Frames\Server_Stats_GUI\byteS-F_FS_Input.toff"

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)))
Comments