473,388 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Occasional OSError: [Errno 13] Permission denied on Windows

Using Python 2.3.5 on Windows XP, I occasionally get OSError: [Errno
13] Permission denied when calling os.remove(). This can occur with a
file that is not used by any other process on the machine, and is
created by the python.exe invocation that is trying to delete it. It
can happen with various pieces of my system - almost anywhere I try to
delete a file.

I have assumed that the problem is that I was holding on to a handle to
the file that I was trying to remove. I have scoured my code and close
any handle to the file that I can find. The intermittent nature of
this problem leads me to believe that I'm not explicitly holding onto a
file object somewhere.

My next theory was that there was some object holding onto a file
handle for which there wasn't an extant reference, but which hadn't
been garbage-collected. So, I tried removing files like this:

try:
os.remove(strPath)
except OSError:
# Wild guess that garbage collection might clear errno 13
gc.collect()
os.remove(strPath)

This does indeed reduce the frequency of the problem, but it doesn't
make it go away completely. I really need to fix this problem, because
I'm developing an app that won't survive if it fails to delete its
files.

Thanks,
Alec Wysoker

Jan 5 '06 #1
11 7816
Alec Wysoker wrote:
Using Python 2.3.5 on Windows XP, I occasionally get OSError:
[Errno 13] Permission denied when calling os.remove(). This can
occur with a file that is not used by any other process on the
machine, and is created by the python.exe invocation that is
trying to delete it. It can happen with various pieces of my
system - almost anywhere I try to delete a file.


The previous company I worked for had a real-time virus scanner that
would cause problems like this. Drove me nuts because I never could
get them to shut the dang thing off.

Regards,
Pat

Jan 5 '06 #2
[Alec Wysoker]
Using Python 2.3.5 on Windows XP, I occasionally get OSError: [Errno
13] Permission denied when calling os.remove(). This can occur with a
file that is not used by any other process on the machine,
How do you know that?
and is created by the python.exe invocation that is trying to delete it. It
can happen with various pieces of my system - almost anywhere I try to
delete a file.

I have assumed that the problem is that I was holding on to a handle to
the file that I was trying to remove. I have scoured my code and close
any handle to the file that I can find. The intermittent nature of
this problem leads me to believe that I'm not explicitly holding onto a
file object somewhere.

My next theory was that there was some object holding onto a file
handle for which there wasn't an extant reference, but which hadn't
been garbage-collected. So, I tried removing files like this:

try:
os.remove(strPath)
except OSError:
# Wild guess that garbage collection might clear errno 13
gc.collect()
os.remove(strPath)

This does indeed reduce the frequency of the problem, but it doesn't
make it go away completely.


Replace gc.collect() there with a short sleep (say, time.sleep(0.2)),
and see whether that does just as well at reducing the frequency of
the problem. My bet is that it will.

Best guess is that some process you haven't thought about yet _is_
opening the file. For example, what you're seeing is common if
Copernic Desktop Search is installed and its "Index new and modified
files on the fly" option is enabled. Other searching/indexing apps,
"file deletion recovery" services, and even some virus scanners can
have similar effects. That's why I asked at the start how you _know_
no other process is opening the file. The symtoms you describe are
consistent with some background-level utility app/service briefly
opening files for its own purposes.

In that case, anything that burns some time and tries again will work
better. Replacing gc.collect() with time.sleep() is an easy way to
test that hypothesis; because gc.collect() does an all-generations
collection, it can consume measurable time.
Jan 5 '06 #3
Interesting theory. I do have a virus scanner, and also Google Desktop
Search. Sometimes I get this error when running a large suite of unit
tests. Each unit test starts off by cleaning the test output
directory, and failing if it can't do so. I will see many (hundreds?)
tests fail because the file can't be removed, and then eventually the
file will get removed. My feeling is that if a virus scanner or
desktop search were keeping the file open, that it would only do so
briefly. On the other hand, when I'm running unit tests I'm chewing up
a lot of CPU so perhaps it takes longer in those cases.

If we assume that this theory is correct, then what is the solution?
Sleep for a little while and try again?

Jan 5 '06 #4
>> Using Python 2.3.5 on Windows XP, I occasionally get OSError: [Errno
13] Permission denied when calling os.remove(). This can occur with a
file that is not used by any other process on the machine,


How do you know that?


Yeah, good point. I don't really know. I should have said no process
that I am aware of is using the file. I'll give sleep a try instead of
gc. Thanks.

Jan 5 '06 #5
On 2006-01-05, Alec Wysoker <al***@pobox.com> wrote:
Using Python 2.3.5 on Windows XP, I occasionally get OSError: [Errno
13] Permission denied when calling os.remove(). This can occur with a
file that is not used by any other process on the machine,


How do you know that?


Yeah, good point. I don't really know. I should have said no process
that I am aware of is using the file.


<ObRant>
If you're running XP, there's probably a shocking amount of
stuff going on that you're not aware of. Even if your machine
isn't 0wned by some script kiddie or other bit of spy/adware
you've uknkowingly installed, it's still 0wned by Microsoft.
</ObRant>

--
Grant Edwards grante Yow! Make me look like
at LINDA RONSTADT again!!
visi.com
Jan 5 '06 #6
Alec Wysoker wrote:
Using Python 2.3.5 on Windows XP, I occasionally get OSError: [Errno
13] Permission denied when calling os.remove(). This can occur with a
file that is not used by any other process on the machine,


How do you know that?

Yeah, good point. I don't really know. I should have said no process
that I am aware of is using the file. I'll give sleep a try instead of
gc. Thanks.


FWIW, you could use Filemon from Sysinternals,
http://www.sysinternals.com/Utilities/Filemon.html

If you are there, be sure to also look the other stuff on that side,
Mark Russinovich a has written a lot of very nice Windows utilities.


Jan 5 '06 #7
File attributes may be an issue to. Take look at the recipe at:

http://aspn.activestate.com/ASPN/Coo.../Recipe/303343

which ensures the file attributes are normal before you delete it.

john

Jan 5 '06 #8
Tim Peters wrote:
In that case, anything that burns some time and tries again
will work better. Replacing gc.collect() with time.sleep() is
an easy way to test that hypothesis; because gc.collect()
does an all-generations collection, it can consume measurable time.


An slight enhancement to this hypothesis-tester (which might even
approach being production-worthy) would be to maintain a list of items
which were not deleted on a particular pass. Sleep after each pass,
then try to kill all the items on the list again. Maintain a counter
of the number of passes which have been made since the last time the
undeleted item list shrank (via a completed deletion), and if the total
time exceeds 'x' or the number of passes since a completed deletion
exceeds 'y', then bail and ask the user to help you out.

Regards,
Pat

Jan 5 '06 #9
> File attributes may be an issue to. Take look at the recipe at:
http://aspn.activestate.com/ASPN/Coo.../Recipe/303343 which ensures the file attributes are normal before you delete it.


I don't think file attributes are an issue, because generally I can
manually delete the file in question, and sometimes my app is
eventually able to delete the file. You're not suggesting that some
other process is temporarily changing the file attributes, are you?

Jan 7 '06 #10
I tried something not exactly like this, but in the same spirit. I
don't generally have a list of files I want to delete - just one. I
try to delete it and if I get errno 13 I sleep for a little while (0.2)
and then try again. If the same problem then I add 1 sec to the sleep
time and try again. After sleeping about 10 sec total I give up.

Unfortunately, one of my testers still experiences the problem even
with this fix. I am surprised that a virus checker or file indexer
would hold onto a file for so long. Ugh.

Patrick Maupin wrote:
Tim Peters wrote:
In that case, anything that burns some time and tries again
will work better. Replacing gc.collect() with time.sleep() is
an easy way to test that hypothesis; because gc.collect()
does an all-generations collection, it can consume measurable time.


An slight enhancement to this hypothesis-tester (which might even
approach being production-worthy) would be to maintain a list of items
which were not deleted on a particular pass. Sleep after each pass,
then try to kill all the items on the list again. Maintain a counter
of the number of passes which have been made since the last time the
undeleted item list shrank (via a completed deletion), and if the total
time exceeds 'x' or the number of passes since a completed deletion
exceeds 'y', then bail and ask the user to help you out.

Regards,
Pat


Jan 7 '06 #11
Well, I found a solution to some of these problems. Perhaps this new
solution, combined with the sleeping to allow
virus-checker,file-indexer,etc. will fix the problem.

I opened a file with shelve, and when adding a particular entry found
it to be corrupt. On Windows, shelve uses bsddb.hashopen. I figured I
would just delete the file, lose whatever data was in there, and start
over. It appears that once a bsddb hash format file is opened and
found to be corrupt, you can't delete the file until the process exits!

I tried opening the shelf with flag='n' in order to wipe the contents
of the file, but under the covers that just calls os.unlink, which
fails with the same errno 13.

Ultimately, I found I could clear out the file like this:

db = anydbm.open(strPath, 'c')
if hasattr(db, 'db') and hasattr(db.db, 'truncate'):
db.db.truncate()
db.close()
return True

Pretty ugly but it seems to work.

-Alec
Alec Wysoker wrote:
I tried something not exactly like this, but in the same spirit. I
don't generally have a list of files I want to delete - just one. I
try to delete it and if I get errno 13 I sleep for a little while (0.2)
and then try again. If the same problem then I add 1 sec to the sleep
time and try again. After sleeping about 10 sec total I give up.

Unfortunately, one of my testers still experiences the problem even
with this fix. I am surprised that a virus checker or file indexer
would hold onto a file for so long. Ugh.


Jan 12 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Stéphane Ninin | last post by:
Hello world, I am fighting with what is probably a stupid problem. In a wxpython GUI, here is a method which must read a file, and if the file is not correctly formed rename it and create a...
0
by: Joram Agten | last post by:
Please put me in CC When running the following program I get frequent errors like this one Exception in thread Thread-4: Traceback (most recent call last): File...
4
by: zombek | last post by:
I'm making a small program which takes a folder with images and generates optimized normal-sized images and thumbnails using Python Imaging Lbrary (PIL). The problem is here: ........
4
by: Russell Warren | last post by:
I've been having a hard time tracking down a very intermittent problem where I get a "permission denied" error when trying to rename a file to something that has just been deleted (on win32). ...
6
by: Antoine De Groote | last post by:
Google tells quite some things about it, but none of them are satisfactory. I'm on Windows, and shutil operations (e.g. move, copy) throw Permission denied all the time, for the source files. It...
3
by: John Machin | last post by:
Hi, Here's what's happening: Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32 Type "help", "copyright", "credits" or "license" for more information. | >>import os | >>os.tmpfile()...
0
by: neerajkumar | last post by:
Below is the fragment of the filemon utility log.The result is showing DELETE . After that DKService(diskkeeper) is trying to access the same file.Since i am new to this filemon utility so i dont...
0
by: private.anders | last post by:
Hi David! Really need assistance since I have been struggling with a problem long time now. I am running a web application on a Win 2003 Std (Active Directory). Everything works fine. I have...
0
by: private.anders | last post by:
Really need your assistance since I have been struggling with a problem long time now. I am running a web application on a Win 2003 Std (Active Directory). Everything works fine. I have...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.