2016-05-03 6 views
2

Python 3.5.1でテキストベースのゲームの保存/読み込み関数を作成しようとしていますが、残念ながらこのサイトで有用なコードが見つかりましたパラメータの1つとしてファイルパスが必要です。私はPythonを学んでいます。このサイトと一般的なコーディングの両方に比較的新しいので、有効なファイルパス(そしてこの例では有効なファイルパスの例です!)を入力する方法を助けてください。 (もう一度、私はJon Snowのようなものです)。関連する方法は以下に掲載されています。有効なファイルパスをPythonの関数パラメータとして配置する方法

def __init__(self, name): 
    self.name = name 
    self.maxHealth = 100 
    self.health = self.maxHealth 
    self.baseAttack = 10 
    self.credits = 10 
    self.augs = 0 
    self.weap = ["Basic Nanite Pack"] 
    self.currWeap = ["Basic Nanite Pack"] 
    global playerFile 
    playerFile = {"name":self.name, "health":self.maxHealth, "maxHealth":self.health, "baseAttack":self.baseAttack, 
        "weapon":self.weap, "currWeap":self.currWeap, "credits":self.credits, "augs":self.augs} 

def saveGame(playerFile, filepath): 
    with open(filepath, 'w') as outfile: 
     for name,num in playerFile.items(): 
      outfile.write("{};{};\n".format(num, name)) 

import csv 
def loadGame(filepath): 
    with open(filepath) as infile: 
     # purposeful misspell for variable name 
     playerFil = dict((v,k) for v,k,_ in csv.reader(infile, delimiter=';')) 
    return playerFil 

答えて

0

ファイルパスは、ファイルの開始から終了までのパスです。 例えば、C:\ユーザー\ジョン\これは有効なファイルパスであり、あなたは通常はこのようにそれを渡す必要があります

をmy_document.txt:あなたはいけない場合、実際のファイルのパスを与えること

​​

注意 例:c:\ users \ John これはディレクトリパスであるため、Pythonはファイルを開くことができません。

+0

ありがとうございました!私はそれを働かせた! – 1v0ryh4t

関連する問題