473,692 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete will assure file is deleted?

Hello,

If I use os.remove(fileN ame), 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 2205
"aj****@gmail.c om" <aj****@gmail.c om> writes:
Hello,

If I use os.remove(fileN ame), 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.or g> 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.co m wrote:
Hello,

If I use os.remove(fileN ame), 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.c om" <aj****@gmail.c om>
declaimed the following in comp.lang.pytho n:
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(fileN ame)
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.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
=============== =============== =============== =============== == <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.ne tcom.com/> <

Jul 19 '05 #5
rbt
aj****@gmail.co m 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.co m 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.co m 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.co m 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.or g> writes:
On Tue, 26 Apr 2005 03:40:16 -0700, aj****@gmail.co m 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.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #10

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

Similar topics

3
3204
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 itself uploaded? Here is the delete script I'm using right now, and the error messages I got below.
1
8893
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 automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
11
454
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 as my app is a winmain app this wouldn't be nice. I could use DeleteFile, but you can not use wildcards in this one. How could I do this using Windows functions?
9
10647
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 table from which I want to delete has two foreign keys that references two other tables and it is also referenced by another table. But this shouldn't be a problem, since I set the commit mode to none (or *none) at all places where this makes...
3
2100
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 other tables etc. in other words we must use cascade delete to do
1
1708
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 every second to get new file inside it. Where should i do the delete function? The way to print image is get from msdn. Can tell me where can i refer to other better ways to print the image file. Thank you. Public fileName As String
1
2356
by: nasirmajor | last post by:
dear all, Please any urgent help regarding following code. i have the following code ================================================================= public void Delete(Object sender, DataGridCommandEventArgs e) { 1 int Sid2 = (int)l.DataKeys;
15
5019
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 - but for the sake of this post ... I'm searching for an answer that assumes said decision. If I allocate memory in the class declaration: char buffer;
11
4073
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, CrtdUser and Date And Edit and Delete buttons
0
8604
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
8538
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
9083
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...
0
8961
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8800
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
8801
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...
0
4557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2238
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1957
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.