Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > delete file from python cgi

Reply
Thread Tools

delete file from python cgi

 
 
lamar_air
Guest
Posts: n/a
 
      07-10-2003
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.
 
Reply With Quote
 
 
 
 
Gilles Lenfant
Guest
Posts: n/a
 
      07-10-2003
No direct connection with cgi...

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

--Gilles

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
Posts: n/a
 
      07-10-2003
lamar_air wrote:
> 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

 
Reply With Quote
 
Bartolomé Sintes Marco
Guest
Posts: n/a
 
      07-10-2003
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,


 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      07-10-2003
"Bartolomé Sintes Marco" wrote:
>
> 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
 
Reply With Quote
 
lamar_air
Guest
Posts: n/a
 
      07-11-2003
Peter Hansen <> wrote in message news:<>...
> "Bartolomé Sintes Marco" wrote:
> >
> > 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?
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      07-11-2003
lamar_air wrote:
>
> Peter Hansen <> wrote in message news:<>...
> > "Bartolomé Sintes Marco" wrote:
> > >
> > > 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
 
Reply With Quote
 
lamar_air
Guest
Posts: n/a
 
      07-15-2003
Peter Hansen <> wrote in message news:<>...
> lamar_air wrote:
> >
> > Peter Hansen <> wrote in message news:<>...
> > > "Bartolomé Sintes Marco" wrote:
> > > >
> > > > 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?
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      07-15-2003
lamar_air wrote:
>
> 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
 
Reply With Quote
 
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
Posts: n/a
 
      07-15-2003
lamar_air wrote:
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert exe file of cgi script into .cgi extension. Muzammil C++ 1 08-28-2008 07:50 PM
Trouble with CGI code from Code Example 7.3 of the "Python Interactive CGI Tutorial" epsilon Python 4 08-22-2007 02:41 AM
cgi relay for python cgi script Amir Michail Python 7 10-04-2005 07:11 PM
Python-cgi or Perl-cgi script doubt praba kar Python 1 07-30-2005 08:25 AM
Python CGI - Accepting Input, Invoking Another Process, Ending CGI LarsenMTL Python 4 11-04-2004 05:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57