所以我想在我的遊戲的python排行榜上,我試圖寫入1個文件中的信息到另一個文件。我現在有代碼:從1個文件寫入到另一個python
playerName = "Luke"
playerScore = 12
def functionHighscore():
highscore = open('H:\Year 13\Computer science\leaderboard\practice.txt','r')
for eachline in highscore:
ply_code,ply_name,ply_score=eachline.split(",")
if playerName == ply_name:
ply_score = playerScore
print(ply_name, ply_score)
functionUpdatehighscore(highscore)
def functionUpdatehighscore(highscore):
updatehighscore = open('H:\Year 13\Computer science\leaderboard\updatepractice.txt','w')
for eachline in highscore:
print(eachline)
functionHighscore()
,並將其與錯誤出現:
syntaxError (unicode error) 'Unicodeescape' codec cant decode bytes in position 39-40: truncated \uXXXX escape (line21, offset 27): 'updatehighscore = open('H:\Year 13\Computer science\leaderboard\updatepractice.txt','w')
使用原前綴:'R'H:\年13 \計算機科學\排行榜\ updatepractice.txt''或'\ u'被視爲Unicode轉義。 –
將您的路徑存儲爲變量.i.e'location1 ='H:\ Year 13 \ Computer science \ leaderboard \ practice.txt''和'location2 ='H:\ Year 13 \ Computer science \ leaderboard \ updatedpractice.txt''。用這些'highscore = open(location2.encode('unicode-escape'),'r')'',updat ehighscore = open(loca tion2.encode('unicod e-escape')',' W')'。如果您使用的是Python 2,請使用'string-escape'而不是'unicode-escape' – mondieki