我不断收到读取问题[错误22]无效参数
我试图把(r"F:\Server\ ... "r")
它说:我不断收到读取问题[错误22]无效参数
file.write(float(u) + '\n') TypeError: unsupported operand type(s) for +: 'float' and 'str'.
当我不把r
是,它都将增加一倍\\对我说: :
read issue [Errno 22] Invalid argument: 'F:\\Server\\Frames\\Server_Stats_GUI\x08yteS-F_FS_Input.toff'.
这里是我的代码
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:文件名与转义字符
此错误:
read issue [Errno 22] Invalid argument: 'F:\Server\Frames\Server_Stats_GUI\x08yteS-F_FS_Input.toff'.
是在open()函数。
你的文件名中有一个转义字符。 '\ b'正在评估为'\ x08'(退格)。没有找到该文件,这会引发错误。
要忽略转义字符,您可以双反斜线:
,或者使用R作为前缀字符串:
r"F:\Server\Frames\Server_Stats_GUI\byteS-F_FS_Input.toff"
你已经尝试了第二种方式,其中固定那个问题。
2:类型错误上写()
下一个错误:
file.write(float(u) + '\n') TypeError: unsupported operand type(s) for +: 'float' and 'str'.
在write()函数。
你正在将一个float视为一个字符串。因为它说
file.write("%f\n" % (float(u)))
谢谢你的工作,出色的输出是119649099776.000000只需要找到一种方法来获取.000000 – ToxicLiquidz101
请参阅此处的字符串格式设置文档:https://docs.python.org/2/library/string.html#format-specification-mini-language。您可以指定精度,例如:“%.0f \ n”%(float(3)) –
:您要添加float和string你需要尝试追加新行之前将其转换为字符串:
或使用字符串格式化。把你的float转换成一个字符串:'str(float(u))' – Julien
所以把file.write(float(u)改为file.writestr(floau(u)) – ToxicLiquidz101