473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

delete will assure file is deleted?

Hello,

If I use os.remove(fileName), does it always assure that the code will
move to the next code only if the fileName is deleted completely?

Pujo

Jul 19 '05 #1
14 2163
"aj****@gmail.com" <aj****@gmail.com> writes:
Hello,

If I use os.remove(fileName), does it always assure that the code will
move to the next code only if the fileName is deleted completely?


Hmm. The documdentation doesn't say. A quick test on FreeBSD shows
that if you don't have permission to remove the file, an exception is
raised. A second test shows that you get a the same exception if the
file doesn't exist.

Things in os tend to be tied tightly to the underlying platform. You
might want to test on your platform.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #2
Hello Mike,
I have to know this topic otherwise my program has to check whether the
file / files are already deleted and this is a little bit messy.

Pujo

Jul 19 '05 #3
aj****@gmail.com wrote:
Hello,

If I use os.remove(fileName), does it always assure that the code will
move to the next code only if the fileName is deleted completely?


Yes, it will be deleted, but not necessary completly. Deleting isn't equal
to erasing file's content, so it might be possible to recover deleted file,
plus on some systems deleted files are kept for those processes (and only
for them), who are still using them, and disappear when everyone close
them.

--
Maciej "Fiedzia" Dziardziel (fiedzia (at) fiedzia (dot) prv (dot) pl)
www.fiedzia.prv.pl

Giraffiti: Concrete art spray-painted very, very high.
Jul 19 '05 #4
On 26 Apr 2005 03:40:16 -0700, "aj****@gmail.com" <aj****@gmail.com>
declaimed the following in comp.lang.python:
Hello Mike,
I have to know this topic otherwise my program has to check whether the
file / files are already deleted and this is a little bit messy.
Ah, but this requirement is different from the one you
originally asked.

In either event, the best solution is probably to wrap the call
with a try block...

try:
os.remove(fileName)
except <need to determine what errors can be raised>:
# do whatever you need for the error
except <other error>:
# do whatever this error needs...

If the error is that the file didn't exist to be deleted, you
can probably use a "pass" as the except processing.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 19 '05 #5
rbt
aj****@gmail.com wrote:
Hello Mike,
I have to know this topic otherwise my program has to check whether the
file / files are already deleted and this is a little bit messy.

Pujo


How about checking with isfile() before and after?
Jul 19 '05 #6
On Tue, 26 Apr 2005 03:40:16 -0700, aj****@gmail.com wrote:
Hello Mike,
I have to know this topic otherwise my program has to check whether the
file / files are already deleted and this is a little bit messy.


I would be fairly confident in asserting that assuming the file is there,
you have permissions, etc., basically that the call succeeds, that the
file will be gone.

os.remove, as the module name implies, tells the OS to do something. I
would consider an OS that returned from a "remove" call, but still let you
access that file, highly broken.

You may be concerned that the OS may not write the fact that the file is
deleted to the disk right away. This is very, very possible; OSs have been
doing that for a while. If, for some reason, this is a major concern that
the system might suddenly lose power and the file may not be truly
deleted, you will need to either shut off this feature (it is called
"write-behind caching", and shutting it off, or indeed even having the
feature at all, is OS-dependent), or get a UPS so that the machine has
time to shut down gracefully.

HOWEVER... the only time this is a problem is if you are truly concerned
about the power spontaneously shutting off. The kernel of your operating
system, if indeed it does write-behind caching at all, will make it look
to all programs on the system (not just your own) that the file is
deleted; and thus, in every sense that matters barring spectacular
power failure, it is.

So I say, ever if you've heard of write-behind caching and you are perhaps
worried about it, you do not need to be; it is intended to be fully
transparent to you, and indeed, short of directly asking the OS whether
the feature is on, there should be no practical way of figuring out
whether it is on at all. All it means is significantly better performance
to you programs if they do things like delete a lot of files at once; you
don't need to worry that they might "still be there" even after the
command is done.

This reply somewhat longer than needed for the purposes of education :-)

Jul 19 '05 #7
On Tue, Apr 26, 2005 at 03:13:20PM -0400, Jeremy Bowers wrote:
On Tue, 26 Apr 2005 03:40:16 -0700, aj****@gmail.com wrote:
Hello Mike,
I have to know this topic otherwise my program has to check whether the
file / files are already deleted and this is a little bit messy.
I would be fairly confident in asserting that assuming the file is there,
you have permissions, etc., basically that the call succeeds, that the
file will be gone.

Not exactly. The system call is called remove not by accident. It's not
called delete. So for example if you have a file with multiple names (so called
hard links) the file will not be gone after os.remove('file')

os.remove, as the module name implies, tells the OS to do something. I
would consider an OS that returned from a "remove" call, but still let you
access that file, highly broken.

Well, it has been the normal semantics with Unix for decades. Actually it's
the normal way to create temporary files that will be cleanuped when the program
exits:
f = open("temp")
os.remove("temp")
# now use f

f.close() # frees the temporary file
sys.exit(0) # exit is an implicit close too.

Andreas
Jul 19 '05 #8
On Tue, 26 Apr 2005 21:24:30 +0200, andreas wrote:
On Tue, Apr 26, 2005 at 03:13:20PM -0400, Jeremy Bowers wrote:
On Tue, 26 Apr 2005 03:40:16 -0700, aj****@gmail.com wrote:
> Hello Mike,
> I have to know this topic otherwise my program has to check whether the
> file / files are already deleted and this is a little bit messy.


I would be fairly confident in asserting that assuming the file is there,
you have permissions, etc., basically that the call succeeds, that the
file will be gone.

Not exactly. The system call is called remove not by accident. It's not
called delete. So for example if you have a file with multiple names (so called
hard links) the file will not be gone after os.remove('file')


This gets into another distinction that I didn't want to get into, given
that my message was heavy enough as it is.

But I would say, that even if "file_a" and "file_b" are both (hard) linked
to the same file, and I "remove" "file_a", I am perfectly justified in
saying that "file_a" is gone, deleted, removed, what have you. The file,
the thing we called "file_a", is no longer accessible. That some operating
systems separate "file" from "contents" and thus that I can get at the
contents in some other way doesn't really make that statement untrue;
"file_a" is still gone. "file deleted" hasn't meant "file contents
eliminated from the disk entirely", well, as far as I know, *ever*;
certainly there were undelete operations in DOS, and that's as far back as
I can attest to personally, but I know that "undelete"s could be done
before then, too. In fact one must search in computing for anything to
ever truly be *eliminated*; in more than just file systems, we
de-allocate, re-allocate for something else, and just overwrite. That's a
pervasive pattern.
Jul 19 '05 #9
Jeremy Bowers <je**@jerf.org> writes:
On Tue, 26 Apr 2005 03:40:16 -0700, aj****@gmail.com wrote:
os.remove, as the module name implies, tells the OS to do something. I
would consider an OS that returned from a "remove" call, but still let you
access that file, highly broken.


Um - not if you have permission to read the file, but don't have
permission to remove it. Whatever the "remove" call does in this case,
you *better* have access to it after the fact. The "remove" call on
Unix (uka "unlink") returns even if it can't remove the file: it
returns 0 if it succeeds, and -1 if it doesn't.

os.remove is a little brighter than that. It will throw an OSError
exception if it can't remove the file.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #10
Dennis Lee Bieber <wl*****@ix.netcom.com> writes:
On 26 Apr 2005 03:40:16 -0700, "aj****@gmail.com" <aj****@gmail.com>
declaimed the following in comp.lang.python:
Hello Mike,
I have to know this topic otherwise my program has to check whether the
file / files are already deleted and this is a little bit messy.

Ah, but this requirement is different from the one you
originally asked.

In either event, the best solution is probably to wrap the call
with a try block...

try:
os.remove(fileName)
except <need to determine what errors can be raised>:
# do whatever you need for the error
except <other error>:
# do whatever this error needs...

If the error is that the file didn't exist to be deleted, you
can probably use a "pass" as the except processing.


This is just a little bit tricky. os.remove (on FreeBSD 5-STABLE,
anyway) throws an OSError exception if it doesn't have permission to
remove the file, *or* if the file doesn't exist. You have to examine
the exception for it's value, which is the result of a strerror
call. I believe that the result of strerror is platform dependent.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #11
On Tue, 26 Apr 2005 21:24:06 -0500, Mike Meyer wrote:
Jeremy Bowers <je**@jerf.org> writes:
On Tue, 26 Apr 2005 03:40:16 -0700, aj****@gmail.com wrote:
os.remove, as the module name implies, tells the OS to do something. I
would consider an OS that returned from a "remove" call, but still let you
access that file, highly broken.


Um - not if you have permission to read the file, but don't have
permission to remove it.


You snipped:
assuming the file is there, you have permissions, etc., basically that
the call succeeds,


First paragraph.

Jul 19 '05 #12
On Tue, 26 Apr 2005 21:33:52 -0500, rumours say that Mike Meyer
<mw*@mired.org> might have written:
This is just a little bit tricky. os.remove (on FreeBSD 5-STABLE,
anyway) throws an OSError exception if it doesn't have permission to
remove the file, *or* if the file doesn't exist. You have to examine
the exception for it's value, which is the result of a strerror
call. I believe that the result of strerror is platform dependent.


Although I don't have experience with FreeBSD, so far checking the
exception's errno args does the job. Example:

import errno

try:
...
except OSError, exc:
if exc.errno == errno.ENOENT: # file inexistant
...
elif exc.errno == errno.EPERM: # no permissions
...
else:
raise
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 19 '05 #13
Christos TZOTZIOY Georgiou wrote:
Although I don't have experience with FreeBSD, so far checking the
exception's errno args does the job.
if that doesn't work on FreeBSD, FreeBSD is not a proper Unix.
import errno

try:
...
except OSError, exc:
if exc.errno == errno.ENOENT: # file inexistant
...
elif exc.errno == errno.EPERM: # no permissions
...


make that

elif exc.errno in (errno.EACCES, errno.EPERM): # no permissions

</F>

Jul 19 '05 #14
On Wed, 27 Apr 2005 17:28:04 +0200, rumours say that "Fredrik Lundh"
<fr*****@pythonware.com> might have written:
import errno

try:
...
except OSError, exc:
if exc.errno == errno.ENOENT: # file inexistant
...
elif exc.errno == errno.EPERM: # no permissions
...
make that

elif exc.errno in (errno.EACCES, errno.EPERM): # no permissions


Yep, you're right (you wouldn't be a bot otherwise, right?-)

BTW I remember a post last summer about subclassing OSError (found it:
http://groups.google.com.gr/groups?s...sf%40pobox.com --not
exactly what I remembered, but close.)

I think throwing subclasses of OSError based on errno would make life
easier --always assuming that Python requires POSIX conformance on all
platforms. I will give it a try RSN...
--
TZOTZIOY, I speak England very best.
"Be strict when sending and tolerant when receiving." (from RFC1958)
I really should keep that in mind when talking with people, actually...
Jul 19 '05 #15

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

Similar topics

3
by: lawrence | last post by:
I've two scripts, one to upload images, another to delete them. The upload script works fine, but the delete script has permissions trouble. How can PHP not have permission to delete an image it...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
11
by: Viviana Vc | last post by:
Hi all, I would like to delete from a directory all the files that match: bar*.* I know that I could do for instance: system("del bar*.*"), but this will bring up the command prompt window and...
9
by: Robert Schneider | last post by:
Hi to all, I don't understand that: I try to delete a record via JDBC. But I always get the error SQL7008 with the error code 3. It seems that this has something to do with journaling, since the...
3
by: John Rivers | last post by:
Hello, I think this will apply to alot of web applications: users want the ability to delete a record in table x this record is related to records in other tables and those to others in...
1
by: apple | last post by:
i try to print image file in a directory using PrintDocument. It will raise printPage event to draw image to the printer. The file will be deleted after print and the directory will be checked...
1
by: nasirmajor | last post by:
dear all, Please any urgent help regarding following code. i have the following code ================================================================= public void Delete(Object sender,...
15
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision -...
11
by: Ed Dror | last post by:
Hi there, I'm using ASP.NET 2.0 and SQL Server 2005 with VS 2005 Pro. I have a Price page (my website require login) with GridView with the following columns PriceID, Amount, Approved,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.