Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to record a files path that is open on windows in file manager, vscode, excel, a pdf etc using python?

Tags:

python

as the title suggests i was wondering how i can get python to return a users currently opened files, not just using file manager but also other programs like vs code. i will than timestamp the data and record it. Is this possible? and if so how? Thanks!

i tried this code(which is ai generated):

import psutil
import win32gui
import win32process
import time
import json
from datetime import datetime

class FileTracker:
    def __init__(self):
        self.target_processes = {
            'explorer.exe': 'File Manager',
            'Code.exe': 'VS Code',
            'EXCEL.EXE': 'Excel',
            'AcroRd32.exe': 'Adobe Reader',
            'Acrobat.exe': 'Adobe Acrobat'
        }
        self.tracked_files = {}
    
    def get_open_files(self):
        current_files = {}
        
        for proc in psutil.process_iter(['pid', 'name']):
            try:
                proc_name = proc.info['name']
                if proc_name in self.target_processes:
                    files = proc.open_files()
                    if files:
                        current_files[proc_name] = {
                            'app_name': self.target_processes[proc_name],
                            'files': [f.path for f in files],
                            'timestamp': datetime.now().isoformat()
                        }
            except (psutil.NoSuchProcess, psutil.AccessDenied):
                continue
        
        return current_files
    
    def track_continuously(self, interval=5):
        """Track files every 'interval' seconds"""
        while True:
            current_files = self.get_open_files()
            
            # Log new files
            for proc_name, data in current_files.items():
                if proc_name not in self.tracked_files:
                    self.tracked_files[proc_name] = set()
                
                for file_path in data['files']:
                    if file_path not in self.tracked_files[proc_name]:
                        print(f"New file opened in {data['app_name']}: {file_path}")
                        self.tracked_files[proc_name].add(file_path)
            
            time.sleep(interval)

# Usage
tracker = FileTracker()
print("Currently open files:")
files = tracker.get_open_files()
for proc, data in files.items():
    print(f"\n{data['app_name']} ({proc}):")
    for file_path in data['files']:
        print(f"  - {file_path}")
like image 341
Jack445 Avatar asked Jan 19 '26 03:01

Jack445


1 Answers

Your code is correct but it only monitor current opened files only not History of you files and to monitor this you have to run this script continuously (depends on your need)

If you want history also of your executable files(.exe) then window stores that data in registry so track down that to extract all data of your desired exe files

And for PDFs and FileExplorer files you have Recent files option so track that all

And extract all metadata of those files to get history of your FILES as well with .exe files

like image 107
YASH 69 Avatar answered Jan 20 '26 19:01

YASH 69



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!