check what files are open in Python
I ended up wrapping the built-in file object at the entry point of my program. I found out that I wasn't closing my loggers.
import io
import sys
import builtins
import traceback
from functools import wraps
def opener(old_open):
@wraps(old_open)
def tracking_open(*args, **kw):
file = old_open(*args, **kw)
old_close = file.close
@wraps(old_close)
def close():
old_close()
open_files.remove(file)
file.close = close
file.stack = traceback.extract_stack()
open_files.add(file)
return file
return tracking_open
def print_open_files():
print(f'### {len(open_files)} OPEN FILES: [{", ".join(f.name for f in open_files)}]', file=sys.stderr)
for file in open_files:
print(f'Open file {file.name}:\n{"".join(traceback.format_list(file.stack))}', file=sys.stderr)
open_files = set()
io.open = opener(io.open)
builtins.open = opener(builtins.open)
List all currently open file handles?
The nice way of doing this would be to modify your code to keep track of when it opens a file:
def log_open( *args, **kwargs ):
print( "Opening a file..." )
print( *args, **kwargs )
return open( *args, **kwargs )
Then, use log_open
instead of open
to open files. You could even do something more hacky, like modifying the File
class to log itself. That's covered in the linked question above.
There's probably a disgusting, filthy hack involving the garbage collector or looking in __dict__
or something, but you don't want to do that unless you absolutely really truly seriously must.
How do I get the list of open file handles by process in C#?
Ouch this is going to be hard to do from managed code.
There is a sample on codeproject
Most of the stuff can be done in interop, but you need a driver to get the filename cause it lives in the kernel's address space. Process Explorer embeds the driver in its resources. Getting this all hooked up from C# and supporting 64bit as well as 32, is going to be a major headache.
List owner processes of open file handles in Windows?
Use processexplorer
http://technet.microsoft.com/en-us/sysinternals/bb896653
From the introduction:
The top window always shows a list of the currently active processes, including the names of their owning accounts, whereas the information displayed in the bottom window depends on the mode that Process Explorer is in: if it is in handle mode you'll see the handles that the process selected in the top window has opened
The handle mode is the one you're interested in.
For Chrome on my box I see for example:
You can also search for a handle by name.
view open file handlers for a process on windows
Process Explorer can provide this information, though it is hidden by default.
To show handles: View -> Lower Pane View -> Handles
Process Explorer also allows you to search on a file name and determine which process has it opened.
Related Topics
What Are the Tkinter Events for Horizontal Edge Scrolling (In Linux)
Maltparser Giving Error in Nltk
A Simple Python Deployment Problem - a Whole World of Pain
Kill Python Interpeter in Linux from the Terminal
Differencebetween C.Utf-8 and En_Us.Utf-8 Locales
Bash: Variable in Single Quote
How to Get Each Dependent Command Execution Output Using Paramiko Exec_Command
Set the Hardware Clock in Python
How to Deal with Linux/Python Dependencies
How to Download Python from Command-Line
High Kernel CPU When Running Multiple Python Programs
Send Sigint in Python to Os.System
Pyodbc:Can't Open the Driver Even If It Exists
Install Tkinter and Python Locally
Python Multiprocessing Memory Usage
How to Cleanly Kill Subprocesses in Python