[Tutor] Can't edit a file I've opened with open(), then closed with close()

Dave Angel d at davea.name
Mon Sep 12 13:51:51 CEST 2011

On 09/12/2011 01:29 AM, Richard D. Moores wrote:
> Win 7, Python 3.2.1
> I'm trying to improve my skills with opening text files, reading from them,
> and writing to them.
> But while I'm doing this sometimes I want to manually modify the text file
> I'm using, C:\t\text.txt . Say I've done this:
>>>> f = open(r"C:\t\test.txt", 'r+')
>>>> f.read()
> '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19Now is
> the time for all goo men\nto come to the aid\nof their party.\n'
>>>> f.close()
> and I decide to manually delete all those integers, and also correct "goo".
> So I open test.txt and do that. But when I try to save, I get "The process
> cannot access the file because it is being used by another process." I'm
> using IDLE, so I close it. But still no save. Why? And what can I do about
> it? I check with the task manager and see 22 pythonw.exe processes..
> Thanks,
> Dick Moores
Nothing wrong with the program as you show it.  Once it you've entered 
the close(), it should release the file so a text editor can work on 
it.  However, I notice you're doing this interactively.  So probably in 
one of the many sessions you DIDN'T close it.  Or you used the wrong 
variable to try to close it.  Until that shell is closed, it'll prevent 
anyone else from writing to the file.

Each of those pythonw processes is a potential problem.  One of them is 
presumably holding onto the file.

Sysinternals has a number of utilities to help track down problems like 
that.  However, in this case, you probably don' t care which process it 
is, just close them all from the task manager.

If you did, the utility 'handle' would probably identify which process 
it was.

Sysinternals, hosted here:

handle is here:



More information about the Tutor mailing list