2016-08-04 79 views
-1

我試圖把(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 
+0

:您要添加float和string你需要嘗試追加新行之前將其轉換爲字符串:

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

或使用字符串格式化。把你的float轉換成一個字符串:'str(float(u))' – Julien

+0

所以把file.write(float(u)改爲file.writestr(floau(u)) – ToxicLiquidz101

回答

0

這裏有兩個單獨的錯誤。

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))) 
+0

謝謝你的工作,出色的輸出是119649099776.000000只需要找到一種方法來獲取.000000 – ToxicLiquidz101

+0

請參閱此處的字符串格式設置文檔:https://docs.python.org/2/library/string.html#format-specification-mini-language。您可以指定精度,例如:「%.0f \ n」%(float(3)) –