Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

os.remove() -> Permission denied

Status
Not open for further replies.

chrus

Programmer
Jul 24, 2003
2
CH
Hello,

In one of my scripts, i am using the remove function to delete a file:

import os
os.remove('Filename')

For one file, i get this error:

Traceback (most recent call last):
File &quot;<interactive input>&quot;, line 1, in ?
OSError: [Errno 13] Permission denied: 'Filename'

It's because the file is protected in writing. Has anybody an idea how i could delete this file? Is there any other function? Or is it possible to change the state of the file on &quot;not protected&quot;?

Thanks for your advice

Chrus
 
If the system says &quot;Permission denied&quot;, it's either because the user you are logged in does not have the rights to delete this file, or this file is still in use by another program.

Changing this file to &quot;not protected&quot; state involves either granting you rights on this file (chmod under Unix, right-click > properties > security under Windows), or identifying the program who is using this file and kill it.

In your programs, it's usually better to handle possible problem, such as not beeing able to delete a file.
You could do it this way:

[tt]
import os
try:
os.remove('Filename')
except OSError:
print 'Cannot delete file. Make sure your have enough credentials to delete this file or that no other process is using this file.'
[/tt]

(You can replace the print... by whatever you think is appropriate when you cannot delete the file.)
 
you could try the following

Code:
os.chmod('C:/autoexec.bat', 0777)

am not sure of the mode value but 0777 removes the
read-only property of files on my system
 
Hello JustinEzequiel,

Thank you for your answer. It's exactely what I needed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top