Skip to content
Advertisement

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 r were it is it will double \ on me saying:

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

Here is my code

import time

while True:
    try:
        file = open("F:ServerFramesServer_Stats_GUIbyteS-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:ServerFramesServer_Stats_GUIbytesS-F_FS_Output","w")    # update the file with the new result
        file.write(float(u) + 'n')
        file.close()
    time.sleep(4)  # wait 4 seconds

Advertisement

Answer

You have two separate errors here.

1: Filename with Escape Characters

This error:

read issue [Errno 22] Invalid argument: ‘F:ServerFramesServer_Stats_GUIx08yteS-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.

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

"F:ServerFramesServer_Stats_GUI\byteS-F_FS_Input.toff"

or b) use r (rawstring) as a prefix to the string:

r"F:ServerFramesServer_Stats_GUIbyteS-F_FS_Input.toff"

You’ve used 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 a) convert it to a string before trying to append the newline:

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

or b) use string formatting:

file.write("%fn" % (float(u)))
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement