2017-03-08 9 views
-3

私は自分のゲームのためにpythonでリーダーボードを作ろうとしています.1ファイルの情報を別のファイルに書き込もうとしています。私は現在のコードがあります。pythonで1ファイルから別のファイルに書き込む

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

未使用の接頭辞「r'H:\ Year 13 \ Computer Science \ leaderboard \ updatepractice.txt」または「\ u」がユニコードのエスケープとして表示されます。 –

+0

あなたのパスを変数.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 2.encode( 'unicod e-escape') ' w ') '。 Python 2を使用している場合は、 'unicode-escape'の代わりに' string-escape'を使用してください。 – mondieki

答えて

0

は、生の文字列を使用してみてください:

highscore = open(r'H:\Year 13\Computer science\leaderboard\practice.txt','r') 

使用
updatehighscore = open(r'H:\Year 13\Computer science\leaderboard\updatepractice.txt','w') 

ファイルパスを持つ生の文字列は常に役に立ちます、otherwiそれぞれ\\\にエスケープする必要があります。

+0

ありがとうございます:) –

+0

なぜ私はそれを置く必要がありますか? –

+0

短い説明を追加(エスケープ '\'から '\\') –

-1

パス文字列の前にr(生)を入れる必要があります。そうしないと、Pythonはバックスラッシュを特殊文字のエスケープとして解釈します。

r'H:\Year 13\Computer science\leaderboard\practice.txt' 
関連する問題