473,671 Members | 2,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete file from python cgi

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.
Jul 18 '05 #1
10 17194
No direct connection with cgi...

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

--Gilles

Jul 18 '05 #2
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

Jul 18 '05 #3
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,
Jul 18 '05 #4
"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
Jul 18 '05 #5
Peter Hansen <pe***@engcorp. com> wrote in message news:<3F******* ********@engcor p.com>...
"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?
Jul 18 '05 #6
lamar_air wrote:

Peter Hansen <pe***@engcorp. com> wrote in message news:<3F******* ********@engcor p.com>...
"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
Jul 18 '05 #7
Peter Hansen <pe***@engcorp. com> wrote in message news:<3F******* ********@engcor p.com>...
lamar_air wrote:

Peter Hansen <pe***@engcorp. com> wrote in message news:<3F******* ********@engcor p.com>...
"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:\I netpub\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:\Ine tpub\wwwroot\Cg i-bin\output.txt' , 'w') so how can i
check if the file exists first?
Jul 18 '05 #8
lamar_air wrote:

I am using this remove method and it works well as long as the file is
there
os.remove("C:\I netpub\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:\Ine tpub\wwwroot\Cg i-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
Jul 18 '05 #9
lamar_air wrote:
I am using this remove method and it works well as long as the file is
there
os.remove("C:\I netpub\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:\Ine tpub\wwwroot\Cg i-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 "insufficie nt 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

Jul 18 '05 #10

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

Similar topics

5
2971
by: DCK | last post by:
Hello I've path to file, which look like this: \\COMPUTER\D$\C++\FILE_TO_DELETE.JPG This path was generated by os.path.walk() function. When i try to delete this file, os.remove() can't find it, os.path.fileexists() can't find it :( I can delete other files (i.e. \\COMPUTER\D$\C\FILE_TO_DELETE.JPG). I guess there's a problem with this C++ directory (++ exacly). I tried to change it to C\+\+, but this still not work. I've no idea how to...
2
2559
by: Oliver Peek | last post by:
Hi all, Probably my fault, I'm opening a Berkeley DB file, iterating through and deleting keys, well making an attempt. I get this error when I try to delete. ---error Traceback (most recent call last): File "./db.py", line 41, in ?
0
1715
by: Peter A. Schott | last post by:
If I want to verify that a file has finished writing before deleting the remote file, what would be the best method? Current code on Python 2.4: #filename - remote FTP server File Name #NewFile - local file copy of the remote file #objFTP - standard ftplib.FTP object NewFile = open(os.path.join(InputPath, RemoteFileName), "wb")
44
4032
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are identical with which. Write a program that prints out which file are redundant copies. Here's the spec. -------------------------- The program is to be used on the command line. Its arguments are one or
16
17005
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums must be UPDATED, if not, they must be INSERTED. Logically then, I would like to SELECT * FROM <TABLE> WHERE ....<Values entered here>, and then IF FOUND UPDATE <TABLE> SET .... <Values entered here> ELSE INSERT INTO <TABLE> VALUES <Values...
9
7032
by: Juergen Huber | last post by:
hello, i am a dummy user in python and new in this programming language and so there will be questions, that´s for you no problem! i never have before programmed in any language! sorry for this! i hope you will help me, that i also could the basics in this language! and later many more, so i hope ! :) the first question for me in this newsgroup will be the follow one: - is there a way to delete in a csv-file the first line?!
2
9273
by: Viewer T. | last post by:
I am trying to write a script that deletes certain files based on certain criteria. What I am trying to do is to automate the process of deleting certain malware files that disguise themselves as system files and hidden files. When I use os.remove() after importing the os module is raises a Windows Error: Access denied probably because the files have disguised themselves as system files. Is there anway to get adequate privileges under...
2
19742
by: Francesco Pietra | last post by:
Please, how to adapt the following script (to delete blank lines) to delete lines containing a specific word, or words? f=open("output.pdb", "r") for line in f: line=line.rstrip() if line: print line f.close()
1
1373
by: Edwin.Madari | last post by:
running this snippet, is blanking out ^abdc$.. what is the issue ? abcd efg hijk lmn $ efg hijk lmn
0
8483
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8401
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8603
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8673
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6236
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2818
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.