delete file from python cgi

Discussion in 'Python' started by lamar_air, Jul 10, 2003.

  1. lamar_air

    lamar_air Guest

    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.
    lamar_air, Jul 10, 2003
    #1
    1. Advertising

  2. No direct connection with cgi...

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

    --Gilles
    Gilles Lenfant, Jul 10, 2003
    #2
    1. Advertising

  3. 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
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jul 10, 2003
    #3
  4. Re: delete file

    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,
    Bartolomé Sintes Marco, Jul 10, 2003
    #4
  5. lamar_air

    Peter Hansen Guest

    Re: delete file

    "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
    Peter Hansen, Jul 10, 2003
    #5
  6. lamar_air

    lamar_air Guest

    Re: delete file

    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?
    lamar_air, Jul 11, 2003
    #6
  7. lamar_air

    Peter Hansen Guest

    Re: delete file

    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
    Peter Hansen, Jul 11, 2003
    #7
  8. lamar_air

    lamar_air Guest

    Re: delete file

    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?
    lamar_air, Jul 15, 2003
    #8
  9. lamar_air

    Peter Hansen Guest

    Re: delete file

    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
    Peter Hansen, Jul 15, 2003
    #9
  10. Re: delete file

    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
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=, Jul 15, 2003
    #10
  11. lamar_air

    David Bolen Guest

    Re: delete file

    Gerhard Häring <> writes:

    > 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
    David Bolen, Jul 15, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    8,388
  2. Matias Surdi

    Delete a file from a CGI

    Matias Surdi, Jun 1, 2007, in forum: Python
    Replies:
    0
    Views:
    360
    Matias Surdi
    Jun 1, 2007
  3. Matias Surdi

    Re: Delete a file from a CGI

    Matias Surdi, Jun 1, 2007, in forum: Python
    Replies:
    0
    Views:
    462
    Matias Surdi
    Jun 1, 2007
  4. Steve Holden

    Re: Delete a file from a CGI

    Steve Holden, Jun 5, 2007, in forum: Python
    Replies:
    0
    Views:
    406
    Steve Holden
    Jun 5, 2007
  5. Muzammil
    Replies:
    1
    Views:
    1,111
    red floyd
    Aug 28, 2008
Loading...

Share This Page