我正在尝试制作多重保存功能。这是我使用的代码
(我知道,global
不是一个正确的编码,但无论如何):
global game
game = None
global file
file = None
# stuff...
def saveGame():
print('► Saving game...')
with open(file, 'wb') as f:
pickle.dump(PlayerIG, f, pickle.HIGHEST_PROTOCOL)
print('► Game saved. You can access it by typing "load" at the title.')
def loadGame():
print('► Loading saved game...')
if os.path.exists(file) == True: # TypeError on this line
with open(file, 'rb') as f:
global PlayerIG
PlayerIG = pickle.load(f)
print('► Game loaded.')
MainMenu()
else:
print('The savefile doesn\'t exist!')
TitleScreen()
fileOpt = input()
if fileOpt == 'back':
TitleScreen()
if fileOpt == '1' or '2' or '3' or '4':
if fileOpt == '1':
file = 'TGHA1.pickle'
if fileOpt == '2':
file = 'TGHA2.pickle'
if fileOpt == '3':
file = 'TGHA3.pickle'
if fileOpt == '4':
file = 'TGHA4.pickle'
if os.path.exists(file) == True:
# code...
subFileOpt = input()
if subFileOpt == 'load':
loadGame()
if subFileOpt == 'delete':
# code...
delFileOpt = input()
if delFileOpt == 'yes':
print('► Deleting file...')
os.remove(file)
print('► File deleted!')
TitleScreen()
# mainly invalid input stuff down from here...
问题是,当我转到# TypeError on this line
的行(loadGame()
函数也可能相同时,它会引发此错误:
TypeError: expected str, bytes or os.PathLike object, not NoneType
我知道这意味着什么,但我该如何解决?