Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

file locked for writing

1 view
Skip to first unread message

Dmitry Teslenko

unread,
May 13, 2008, 10:57:03 AM5/13/08
to pytho...@python.org
Hello!
I use some script in python 2.5 from vim editor (it has python
bindings) that updates some file
and then launches another program (ms visual studio, for example) to
do something with updated file.
I faced problem when updated file is locked for writing until vim
editor is closed.

launch vim -> update file -> launch msvc -> file locked
launch vim -> update file -> launch msvc -> close vim -> file locked
launch vim -> update file -> -> close vim -> launch msvc -> file okay

Update code is something like that:

backup_file_name = '<some file name>'
with open(backup_file_name, 'w') as backup_file:
input = sax.make_parser()
output = saxutils.XMLGenerator(backup_file, 'cp1252')
filter = __vcproj_config_filter('<updated file name>', input, output)
filter.parse('<updated file name>')
shutil.copyfile(backup_file_name, '<updated file name>')
os.remove(backup_file_name)

__vcproj_config_filter is a descent of a XMLFilterBase; it substitutes
some attributes in xml file and that's all.
must be noted that __vcproj_config_filter instance holds reference to
output (sax xml parser) object.

Gabriel Genellina

unread,
May 14, 2008, 12:18:24 AM5/14/08
to pytho...@python.org
En Tue, 13 May 2008 11:57:03 -0300, Dmitry Teslenko <dtes...@gmail.com>
escribió:

Is the code above contained in a function? So all references are released
upon function exit?
If not, you could try using: del input, output, filter
That should release all remaining references to the output file, I
presume. Or locate the inner reference to the output file
(filter.something perhaps?) and explicitely close it.

--
Gabriel Genellina

Dmitry Teslenko

unread,
May 14, 2008, 3:04:19 AM5/14/08
to pytho...@python.org
On Wed, May 14, 2008 at 8:18 AM, Gabriel Genellina
<gags...@yahoo.com.ar> wrote:
> En Tue, 13 May 2008 11:57:03 -0300, Dmitry Teslenko <dtes...@gmail.com>
> Is the code above contained in a function? So all references are released
> upon function exit?

Yes, it's a function

> If not, you could try using: del input, output, filter
> That should release all remaining references to the output file, I presume.
> Or locate the inner reference to the output file (filter.something perhaps?)
> and explicitely close it.

That doesn't help either.
When I've rewrite code something like that:

with open(backup_file_name, 'w') as backup_file:

.....


filter.parse('<updated file name>')

del input, output, filter
os.remove(project.get_vcproj())
os.rename(backup_file_name, project.get_vcproj())

It triggers WindowsError on os.remove()

Dmitry Teslenko

unread,
May 14, 2008, 3:13:48 AM5/14/08
to pytho...@python.org
On Wed, May 14, 2008 at 11:04 AM, Dmitry Teslenko <dtes...@gmail.com> wrote:
> When I've rewrite code something like that:
> with open(backup_file_name, 'w') as backup_file:
> .....
>
> filter.parse('<updated file name>')
> del input, output, filter
> os.remove(project.get_vcproj())
> os.rename(backup_file_name, project.get_vcproj())
>
> It triggers WindowsError on os.remove()

Using "programming by permutation" pattern I've finally solved that
thing: filter
for some reason doesn't close file after XMLFilterBase.parse(<file_name>);
even after del filter; Workaround for this is to pass <file_descriptor>
instead of <file_name> to XMLFilterBase.parse() and then explicitly close
file or put this call in with-block.

0 new messages