Last active
January 12, 2024 20:53
-
-
Save hdary85/b70b35020fc904fb1a1db15e1d0994f2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import psutil | |
| def get_files_in_use(directory): | |
| files_in_use = set() | |
| # Iterate through all files in the directory and its subdirectories | |
| for root, dirs, files in os.walk(directory): | |
| for file in files: | |
| file_path = os.path.join(root, file) | |
| # Check if the file is in use by any process | |
| for process in psutil.process_iter(['pid', 'open_files']): | |
| try: | |
| open_files = process.info['open_files'] | |
| if any(file_path == open_file.path for open_file in open_files): | |
| files_in_use.add(file_path) | |
| break | |
| except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess): | |
| pass | |
| return list(files_in_use) | |
| # Example usage: | |
| directory_path = '/path/to/your/directory' | |
| files_in_use = get_files_in_use(directory_path) | |
| print("Files in use in the directory and its subdirectories:") | |
| for file in files_in_use: | |
| print(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment