当我打开文件时,如何让python告诉我?

时间:2017-03-09 19:29:51

标签: python python-3.x

所以有什么方法可以让python告诉我每当我打开文件?

所以,如果我打开file.txt它会告诉我,我打开它打开了#34;你打开了file.txt"或者某种东西,它会用我打开的任何文件来做到这一点。

示例:

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

所以,如果我打开Skype,那就是:

"you opened: Skype.exe"

如您所见,我显然不知道该怎么做。

我正在使用python 3.4和Windows 8.1。

告诉我是否有任何需要澄清的内容。

2 个答案:

答案 0 :(得分:0)

这应该有效:

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)

阅读List running processes on 64-bit Windows

答案 1 :(得分:0)

这很有用。

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])