简体   繁体   中英

How to writea file in %APPDATA% folder python

I'm trying to write a file in %appdata% but when I run the code I get this error:

    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'

Here's the code I'm using:


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). You are opening file in a wrong way.
2). The Correct way is given below.

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

Just open it in 'a' mode. The file is created if it does not exist.

use with open(os.path.join(key_dir+ "\\\\key_capture.txt",'a')) as f: instead of with open(os.path.join(key_dir+ "\\\\key_capture.txt")) as f:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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