-4

So is there any way i can have python tell me whenever i open a file?

So if i open file.txt it will tell me that i opened it by printing out "you opened file.txt" or something, and that it would do this with whatever file i opened.

Example:

file = any files in the entire OS
if file == open:
    print("you opened:", file)

so if i opened skype it would be:

"you opened: Skype.exe"

As you can see i have clearly no idea how to do this.

Im using python 3.4 and windows 8.1.

Tell me if there is anything i need to clarify.

Lojas
  • 195
  • 3
  • 13
  • 7
    ... Don't you already *know* when you do these things? – juanpa.arrivillaga Mar 09 '17 at 19:31
  • 1
    Do you mean you want Python pop up and tell you: "Whoa, you opened a file called !" _whenever you open a file anywhere in your OS_? This is difficult to accomplish, to my mind, if not impossible. You may have better chances to do this in C, though. – ForceBru Mar 09 '17 at 19:33
  • @Carcigenicate I'm not sure you understood the question. I just want to get notified whenever any file opens on my computer. – Lojas Mar 09 '17 at 19:33
  • @ForceBru Yes indeed. – Lojas Mar 09 '17 at 19:34
  • @Lojas you might want to look into writing a filesystem watcher, that way your python script is notified on changes. – Pablo Canseco Mar 09 '17 at 19:34
  • 1
    No, I'm not sure you wrote a clear question. – Ken White Mar 09 '17 at 19:34
  • @ForceBru I imagine on Windows you would use something like C#. – juanpa.arrivillaga Mar 09 '17 at 19:37
  • I think the ambiguity here is that "open" can mean "preparing a file so that the bytes it contains can be read into a buffer", or it can mean "executing a program, perhaps by double-clicking on a file's icon". The former is usually what is meant in a programming context, but I think the latter is what OP means. – Kevin Mar 09 '17 at 19:39
  • For Windows: https://pypi.python.org/pypi/watcher, for Linux: https://pypi.python.org/pypi/pyinotify/0.9.6 – cdarke Mar 09 '17 at 19:50

2 Answers2

0

This is supposed to work:

import wmi
import time
c = wmi.WMI ()

def check_process():
    for process in c.Win32_Process ():
        L = []
        L.append(process.Name)

while True:
    check_process()
    L2 = L[:]
    time.sleep(1) # check every second
    check_process()
    if set(L2) & set(L) != set():
        print ('you opened:',process.Name)

Read List running processes on 64-bit Windows

Community
  • 1
  • 1
Ruan
  • 65
  • 1
  • 1
  • 9
0

This worked.

import wmi
import time
c = wmi.WMI ()



while True:

    for process in c.Win32_Process ():

        proccesses = process.Name
    time.sleep(1)

    file = open("proccesses.txt", 'w')
    file.write("%s" % (proccesses))
    file.close()

    file = open("proccesses.txt", "r")
    lastProccess = [line.split(',') for line in file.readlines()]
    print(lastProccess[-1])
Lojas
  • 195
  • 3
  • 13