How to get rid of many open file errors when quitting debugger

  1. System: Windows 11 upgraded from Windows 10 with Python 3.14. Our programs on on another network drive which is E:
  2. My program is e:\myprog\cleardupe.py
  3. My programs always begin with r""" """ on the first line where I put many comments about what the program does. I use the r because sometimes I have a Windows path with a backslash and using r removes errors from that.
  4. I start my program in a .bat file like this: python -m pdb cleardupe.py --inputfile:"inputfiles\My file with spaces.xlsx" --overwrite

Each time I exit the debugger I seem to get 3 tracebacks which are ValueError: I/O operation on closed file but those make no sense. The program completes normally. At the end of the program I write a log file like this and the log file appears to be closed correctly.

if not os.path.exists(options.logfn):
    file = open(options.logfn, 'w') # Open a text file.
    content = listjoin(hdr)
    file.write(content) # Write file header. 
else: 
    file = open(options.logfn, 'a') # Open a text file for append.
    content = listjoin(mylist) # \n is CRLF.
    file.write("\n" + content) # \n needed before new line is added. 

file.close() # Always close file when done.

Sorry! Here are the errors.

> e:\myprog\cleardupe.py(2)<module>()
-> r""" This is a template for othe programs.
(Pdb) q
*** SystemExit: None
(Pdb) Traceback (most recent call last):
  File "C:\Program Files\Python314\Lib\pdb.py", line 3601, in main
    pdb._run(target)
    ~~~~~~~~^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 2522, in _run
    self.run(target.code)
    ~~~~~~~~^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 899, in run
    exec(cmd, globals, locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "E:\myprog\cleardupe.py", line 2, in <module>
    r""" This is a template for othe programs.
    ...<220 lines>...
    """
  File "C:\Program Files\Python314\Lib\bdb.py", line 94, in wrapper
    ret = func(frame, *args)
  File "C:\Program Files\Python314\Lib\bdb.py", line 130, in line_callback
    frame.f_trace(frame, 'line', None)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 284, in trace_dispatch
    return self.dispatch_line(frame)
           ~~~~~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 310, in dispatch_line
    self.user_line(frame)
    ~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 547, in user_line
    self.interaction(frame, None)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
    self._cmdloop()
    ~~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
    self.cmdloop()
    ~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.
Uncaught exception. Entering post mortem debugging
Running 'cont' or 'step' will restart the program
> c:\program files\python314\lib\cmd.py(137)cmdloop()
-> line = input(self.prompt)
(Pdb) Traceback (most recent call last):
  File "C:\Program Files\Python314\Lib\pdb.py", line 3601, in main
    pdb._run(target)
    ~~~~~~~~^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 2522, in _run
    self.run(target.code)
    ~~~~~~~~^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 899, in run
    exec(cmd, globals, locals)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
  File "E:\myprog\cleardupe.py", line 2, in <module>
    r""" This is a template for othe programs.
    ...<220 lines>...
    """
  File "C:\Program Files\Python314\Lib\bdb.py", line 94, in wrapper
    ret = func(frame, *args)
  File "C:\Program Files\Python314\Lib\bdb.py", line 130, in line_callback
    frame.f_trace(frame, 'line', None)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 284, in trace_dispatch
    return self.dispatch_line(frame)
           ~~~~~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\Python314\Lib\bdb.py", line 310, in dispatch_line
    self.user_line(frame)
    ~~~~~~~~~~~~~~^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 547, in user_line
    self.interaction(frame, None)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
    self._cmdloop()
    ~~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
    self.cmdloop()
    ~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "C:\Program Files\Python314\Lib\pdb.py", line 3627, in <module>
    pdb.main()
    ~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 3614, in main
    pdb.interaction(None, e)
    ~~~~~~~~~~~~~~~^^^^^^^^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 736, in interaction
    self._cmdloop()
    ~~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\pdb.py", line 601, in _cmdloop
    self.cmdloop()
    ~~~~~~~~~~~~^^
  File "C:\Program Files\Python314\Lib\cmd.py", line 137, in cmdloop
    line = input(self.prompt)
ValueError: I/O operation on closed file.

Python 3.11 did not do this. How do I get rid of all these errors?

Thank you.

Can you try using the with context manager instead since it takes care of closing the file automatically on exit.

if not os.path.exists(options.logfn):
    with open(options.logfn, 'w') as file:
        content = listjoin(hdr)
        file.write(content)  # Write file header.

else:
    with open(options.logfn, 'a') as file:
        content = listjoin(mylist)
        file.write("\n" + content)  # Write file header.

You can also wrap it in an exception handler pair:

try:
   # the statements above here

except ValueError:
    print('Attempted file access after file closed.')

Thank you for the reply. There is no change. I still get those errors. I wonder if this is a bug related to debugging mode.

The interesting part is an error here at the top of my program line 2:

  File "E:\GilsonJobs\Test\CommFirst\333681delpink-20251031\cleardupe.py", line 2, in <module>
    r"""
    ...<256 lines>...
    """

Changing the first 2 lines to this still gives me errors:

 
""" 
Created on:  by xxx@email.com

And using single triple quotes ''' gives me the errors as well.

'''
Created on:  by xxx@email.com

Install these with: python -m pip install keyring  python-dotenv pandas xlsxwriter openpyxl

import inspect # My note for getting function name.
    procname = str(inspect.stack()[0][3]) + ":"

(many more lines here)
# I have some markdown headers inside the triple quotes as well. Like this.
'''

Ok this might be a bug in Python 3.14. I found another open file, and closed it manually. Then printed the file handle, the file handle was still available and not closed. Even if I use a WITH clause the file handle is still available after the WITH clause. When I’m in the debugger and close the file handle with errfile.close() then the file handle is released.

Here’s the simplest program to reproduce this.

"""errfile file handle is not being closed and causes 3 errors when exiting debugger every time.
"""

import os

errfn = "errtest.txt"
with open(errfn, 'w') as errfile: 
    errfile.write("This is a test line\n")
    
print(f"1) errfile={errfile}")

errfile = open(errfn, 'w')
errfile.write("This is test line 2\n")
errfile.close()

print(f"2) errfile={errfile}")

Whether I run this in the pdb debugger or not the print statement still shows the file handle as available/populated when it should be closed.

  1. Is there a way to escalate this to the programmers for them to check?
  2. Who should I ping for this to get their attention?
  3. Should I post this in another area on this site? Which one?

Works for me on macOS, I added printing the closed state.

import os

errfn = "errtest.txt"
with open(errfn, 'w') as errfile:
    errfile.write("This is a test line\n")

print(f"1) errfile={errfile} closed {errfile.closed}")

errfile = open(errfn, 'w')
errfile.write("This is test line 2\n")
errfile.close()

print(f"2) errfile={errfile} closed {errfile.closed}")

And the output I see

% python3.14 a.py
1) errfile=<_io.TextIOWrapper name='errtest.txt' mode='w' encoding='UTF-8'> closed True
2) errfile=<_io.TextIOWrapper name='errtest.txt' mode='w' encoding='UTF-8'> closed True