delete file from python cgi

L

lamar_air

I have a python script and i want to delete a file on my computer
c:\....\...\.. .txt what's the script to do this? I know how to write
to files but i also need to know how to delete a file.
 
G

Gilles Lenfant

No direct connection with cgi...

import os
os.remove("/path/to/your/file.txt")

--Gilles
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

lamar_air said:
I have a python script and i want to delete a file on my computer
c:\....\...\.. .txt what's the script to do this? I know how to write
to files but i also need to know how to delete a file.

os.unlink() or os.remove(). They're the same.

-- Gerhard
 
B

Bartolomé Sintes Marco

I have a program that unzip some zip files. After unzipping them,
I want to delete the zip files with os.remove, but I get this error:
OSError: [Errno 13] Permission denied: ....
What can I do?

Thanks,
 
P

Peter Hansen

Bartolomé Sintes Marco said:
I have a program that unzip some zip files. After unzipping them,
I want to delete the zip files with os.remove, but I get this error:
OSError: [Errno 13] Permission denied: ....

Close the files properly after unzipping them, perhaps? If that
doesn't help, please specify operating system, and give details
on how you are unzipping. Is it using Python's zipfile module,
or some other method, and exactly how do you do it?

-Peter
 
L

lamar_air

Peter Hansen said:
Bartolomé Sintes Marco said:
I have a program that unzip some zip files. After unzipping them,
I want to delete the zip files with os.remove, but I get this error:
OSError: [Errno 13] Permission denied: ....

Close the files properly after unzipping them, perhaps? If that
doesn't help, please specify operating system, and give details
on how you are unzipping. Is it using Python's zipfile module,
or some other method, and exactly how do you do it?

-Peter

I want to be able to delete a file on my C: drive through my python
script. My script writes a file. So i want to delete the file if it
already exists then write it. What's the best way to do this?
 
P

Peter Hansen

lamar_air said:
Peter Hansen said:
Bartolomé Sintes Marco said:
I have a program that unzip some zip files. After unzipping them,
I want to delete the zip files with os.remove, but I get this error:
OSError: [Errno 13] Permission denied: ....

Close the files properly after unzipping them, perhaps? If that
doesn't help, please specify operating system, and give details
on how you are unzipping. Is it using Python's zipfile module,
or some other method, and exactly how do you do it?

I want to be able to delete a file on my C: drive through my python
script. My script writes a file. So i want to delete the file if it
already exists then write it. What's the best way to do this?

Your question is still not clear, if it's the same as the original
question. Please read http://www.catb.org/~esr/faqs/smart-questions.html
for assistance in formulating your questions a little better.

The simplest answer is that if *you* are creating the files, you
just open them with "w" mode (as in "f = open('filename', 'w')")
and it will automatically truncate the file if it already exists.

Hoping that helps,
-Peter
 
L

lamar_air

Peter Hansen said:
lamar_air said:
Peter Hansen said:
:

I have a program that unzip some zip files. After unzipping them,
I want to delete the zip files with os.remove, but I get this error:
OSError: [Errno 13] Permission denied: ....

Close the files properly after unzipping them, perhaps? If that
doesn't help, please specify operating system, and give details
on how you are unzipping. Is it using Python's zipfile module,
or some other method, and exactly how do you do it?

I want to be able to delete a file on my C: drive through my python
script. My script writes a file. So i want to delete the file if it
already exists then write it. What's the best way to do this?

Your question is still not clear, if it's the same as the original
question. Please read http://www.catb.org/~esr/faqs/smart-questions.html
for assistance in formulating your questions a little better.

The simplest answer is that if *you* are creating the files, you
just open them with "w" mode (as in "f = open('filename', 'w')")
and it will automatically truncate the file if it already exists.

Hoping that helps,
-Peter

I am using this remove method and it works well as long as the file is
there
os.remove("C:\Inetpub\wwwroot\Cgi-bin\output.txt")
If the file doesn't exist then i get an error that the file is not
found. I want to stay away from using
f2=open('C:\Inetpub\wwwroot\Cgi-bin\output.txt', 'w') so how can i
check if the file exists first?
 
P

Peter Hansen

lamar_air said:
I am using this remove method and it works well as long as the file is
there
os.remove("C:\Inetpub\wwwroot\Cgi-bin\output.txt")
If the file doesn't exist then i get an error that the file is not
found. I want to stay away from using
f2=open('C:\Inetpub\wwwroot\Cgi-bin\output.txt', 'w') so how can i
check if the file exists first?

I'm still confused. You "want to stay away from using open(xx)"
but you are getting an error from os.remove when the file doesn't
exist.

Why do you want to stay away from open() when it would clearly fix
the problem? It does not raise an exception when the file doesn't
exist, and it quietly truncates (effectively removes) the file when
it already does exist.

Anyway, if you insist on using os.remove(), just put it in a
try/except OSError block and catch the exception that is thrown
when the file does not exist.

Alternatively, and the worst of all the solutions, is to use
os.path.exists() first to check if the file already exists, and
only then to call os.remove().

The choice is yours. Personally, I'd go with open(xxx, 'w') since
that's safer, idiomatic (i.e. standard usage), and much simpler.

-Peter
 
?

=?ISO-8859-1?Q?Gerhard_H=E4ring?=

lamar_air said:
I am using this remove method and it works well as long as the file is
there
os.remove("C:\Inetpub\wwwroot\Cgi-bin\output.txt")
If the file doesn't exist then i get an error that the file is not
found. I want to stay away from using
f2=open('C:\Inetpub\wwwroot\Cgi-bin\output.txt', 'w') so how can i
check if the file exists first?

if not os.path.exists("foo"):
os.remove("foo")

or just catch the error, with something like:

try:
os.remove("foo")
except OSError, detail:
if detail.errno != 2:
raise

This will ensure that only the "file not found case" is ignored, not for
example the "insufficient permission" case. Not that I know where this
error numbers comes from, I just experimented ;-) If anybody could tell
me where I can find those error numbers without RTFMing myself I'd be
grateful.

-- Gerhard
 
D

David Bolen

Gerhard Häring said:
if not os.path.exists("foo"):
os.remove("foo")

Wouldn't you want this to be a positive check (e.g., without "not")?
or just catch the error, with something like:

try:
os.remove("foo")
except OSError, detail:
if detail.errno != 2:
raise

For the OP, the second option should in general be preferred, since
even in the first case, the file might disappear between when the
exists() call is made and when the remove() call is made.

Or put another way, even if you do want to use os.path.exists() to
conditionalize the execution of some code, you'll need to realize that
the check won't guarantee that the file remains present for any
specific length of time following the check. So for maximum
robustness you'd need to protect the os.remove() call anyway, and
might as well dispense with the initial check and just attempt the
operation.

-- David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top