在Python中使用时间模块

2024-05-15 00:16:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为'time'的文本文件,每次玩家玩游戏(Snake和ladder)时,我都想把当前的日期和时间保存在文本文件中。 请帮我想一下。 我试着为它做一个函数,在文件中添加当前日期,并在用户玩游戏时在文本文件中附加新的日期和时间,但每次都会发生错误


Tags: 文件函数用户time错误时间玩家玩游戏
2条回答

请参考以下帖子:How do you append to a file in Python?

如果您自己实现了Snake and ladder游戏,只需将本文中显示的代码部分添加到脚本中即可。另一方面,如果您没有访问游戏代码的权限,则可以在游戏运行时跟踪进程并运行跟踪脚本。 (Constantly monitor a program/process using Python

干杯

使用json包执行此操作:

import json
def save(directory: str,data: str):
    try:
        with open(directory) as file:#Note that the format of your file should be .json and it can also be .txt
            json.dump(data, file)
    except FileNotFoundError:
        return "no such directory or file found"

def show(directory: str):
    try:
        with open(directory) as file:  # Note that the format of your file should be .json
            json.load(file)
    except FileNotFoundError:
        return "no such directory or file found"

相关问题 更多 >

    热门问题