Python 我不断收到一个读取问题[Errno 22]无效参数

Python 我不断收到一个读取问题[Errno 22]无效参数,python,windows,Python,Windows,我试着把(r“F:\Server\…“r”)放在上面,上面写着: file.write(float(u) + '\n') TypeError: unsupported operand type(s) for +: 'float' and 'str'. 这是我的密码 import time while True: try: file = open("F:\Server\Frames\Server_Stats_GUI\byteS-F_FS_Input.toff","r")

我试着把
(r“F:\Server\…“r”)
放在上面,上面写着:

file.write(float(u) + '\n') TypeError: unsupported operand type(s) for +: 'float' and 'str'. 这是我的密码

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

这里有两个不同的错误

1:带有转义字符的文件名 此错误:

读取问题[Errno 22]无效参数: 'F:\Server\Frames\Server\u Stats\u GUI\x08yteS-F\u FS\u Input.toff'

…位于
open()
函数中

您的文件名中有转义字符。“\b”将被计算为“\x08”(退格)。找不到该文件,这会引发错误

a) 要忽略转义字符,可以将反斜杠加倍:

"F:\Server\Frames\Server_Stats_GUI\\byteS-F_FS_Input.toff"
或b)使用
r
(rawstring)作为字符串的前缀:

r"F:\Server\Frames\Server_Stats_GUI\byteS-F_FS_Input.toff"
您使用了第二种方法,解决了这个问题

2:write()上的类型错误 下一个错误:

file.write(float(u)+'\n')类型错误:不支持的操作数类型 +:“float”和“str”

位于
write()
函数中

您将浮点数视为字符串。在尝试附加换行符之前,您需要a)将其转换为字符串:

file.write(str(float(u)) + '\n')
或b)使用字符串格式:

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

正如上面所说:您正在尝试添加浮点和字符串。将浮点转换为字符串:
str(float(u))
因此更改文件。write(float(u)到file.writestr(floau(u))谢谢,效果很好输出为119649099776.000000只需找到一种方法来使用.000000。请参阅此处的字符串格式文档:。您可以指定精度,例如:“%.0f\n”%(浮点(3))
file.write("%f\n" % (float(u)))