I want to check if a folder named "foldername " is empty.
I use os.listdir(fold ername)==[] to do this,
but it will be very slow if the folder has a lot of sub-files.
Is there any efficient ways to do this?
Thanks~ 13 33676
could ildg: I want to check if a folder named "foldername " is empty. I use os.listdir(fold ername)==[] to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do this?
The first thing to do is measure the performance of this operation
in the context of your application to see if it is really worth extra
effort.
Tweaking this will depend on the platform. On Windows you can use
ctypes and the system functions FindFirstFile/FindNextFile/FindClose,
finishing once you see a single file. Directories always contain "." and
".." entries so they should be ignored. On other platforms there will be
similar low level functions.
Neil
Thank you .
so you mean that this is not platform-independent?
On 8/8/05, Neil Hodgson <ny************ *****@gmail.com > wrote: could ildg: I want to check if a folder named "foldername " is empty. I use os.listdir(fold ername)==[] to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do this? The first thing to do is measure the performance of this operation in the context of your application to see if it is really worth extra effort. Tweaking this will depend on the platform. On Windows you can use ctypes and the system functions FindFirstFile/FindNextFile/FindClose, finishing once you see a single file. Directories always contain "." and ".." entries so they should be ignored. On other platforms there will be similar low level functions. Neil -- http://mail.python.org/mailman/listinfo/python-list
could ildg wrote: I want to check if a folder named "foldername " is empty. I use os.listdir(fold ername)==[] to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do this?
try:
os.rmdir(path)
empty = True
except OSError:
empty = False
should be efficient. A directory (please stop calling them "folders")
can only be removed if it's empty.
Reinhold
Reinhold Birkenfeld wrote: could ildg wrote:
I want to check if a folder named "foldername " is empty. I use os.listdir(fold ername)==[] to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do this?
try: os.rmdir(path) empty = True except OSError: empty = False
should be efficient. A directory (please stop calling them "folders") can only be removed if it's empty.
Reinhold
Unfortunately that destroys the directory if it was empty,
and then you can't recreate it unless (a) you went to the
trouble of preserving all of its attributes before deleting
it, and (b) your script has OS-level permissions to recreate
the directory with its original attributes (such as owners
and permissions).
Also note that modifying a filesystem can be significantly
slower than reading from it (it varies widely across platforms).
-- James
could ildg so you mean that this is not platform-independent?
Your existing code is platform-independent but I think for faster
code you will need to write platform-dependent code. FindFirstFile is
only available on Windows.
Neil
Reinhold Birkenfeld wrote: could ildg wrote:
I want to check if a folder named "foldername " is empty. I use os.listdir(fold ername)==[] to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do this?
try: os.rmdir(path) empty = True except OSError: empty = False
should be efficient.
But it removes the folder if it is empty. Perhaps it's not efficient to
create the folder again.
could ildg wrote: I want to check if a folder named "foldername " is empty. I use os.listdir(fold ername)==[] to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do this?
I'm just curious to know under what circumstances where it's important
to know whether a directory is empty it's also important that the
operation occur with lightning speed...
Reinhold's suggestion to delete the folder was interesting in this
respect... isn't that (prior to deleting a folder) just about the only
time one cares if it's empty, normally? And in this case you don't need
to do the check, as Reinhard shows, so performance isn't an issue.
-Peter
James Dennett <jd******@acm.o rg> writes: Reinhold Birkenfeld wrote:
could ildg wrote:
I want to check if a folder named "foldername " is empty. I use os.listdir(fold ername)==[] to do this, but it will be very slow if the folder has a lot of sub-files. Is there any efficient ways to do this? try: os.rmdir(path) empty = True except OSError: empty = False should be efficient. A directory (please stop calling them "folders") can only be removed if it's empty. Reinhold
Unfortunately that destroys the directory if it was empty, and then you can't recreate it unless (a) you went to the trouble of preserving all of its attributes before deleting it, and (b) your script has OS-level permissions to recreate the directory with its original attributes (such as owners and permissions).
It's also buggy, in that there are other things that can cause an
OSError: import os os.rmdir("/tmp/x")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
OSError: [Errno 1] Operation not permitted: '/tmp/x' ^D
bhuda% bhuda% ls -a /tmp/x
.. ..
The problem here is that I don't own /tmp/x, so I can't delete it,
hence I get an OSError even though it's empty.
Just out of curiosity, is there an OS out there where you can have the
permissions needed to delete a directory without having the
permissions needed to create it appropriately?
<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
On Mon, 08 Aug 2005 09:03:39 -0400,
Mike Meyer <mw*@mired.or g> wrote: Just out of curiosity, is there an OS out there where you can have the permissions needed to delete a directory without having the permissions needed to create it appropriately?
Depending on your definition of "out there," yes, some OS's grant those
privileges separately. Apollo (eventually bought out by HP) Aegis
(mostly like *nix, but just different enough to trip me up again and
again) used to be one. The more secure the environment, the more likely
certain privileges do *not* imply others.
Regards,
Dan
--
Dan Sommers
<http://www.tombstoneze ro.net/dan/> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: atse |
last post by:
Hi,
I still don't know how to determine a empty folder. Can you tell me more
detail? thanks
Atse
"Ray at <%=sLocation%>" <myfirstname at lane34 dot com> wrote in message
news:OriKxw1kDHA.2272@tk2msftngp13.phx.gbl...
> Set oFSO = Server.CreateObject("Scripting.FileSystemObject")
|
by: Keith |
last post by:
I have a script which allows a user to upload and delete files from the
server.
Is it possible (and if so how) to do a check to see if the folder is empty
(ie if the last file in a particular folder is deleted by a user) and if it
is empty, delete the folder?
Thanks
|
by: Web Webon |
last post by:
Hi everybody!
I wonder if this is possible?
I need to determine if a client is using "windows classic folders" or
anything else. If I instantiate a Shell ActiveX object is there a way
of obtaining this information from javascript? (I know that the user
will get prompted about allowing such an operation, but I am willing
to live with this). Because of the way one of my pages work, I need
to know this information in order to "cover up"...
|
by: Pmcg |
last post by:
Hi,
I've been using
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) to get
the application data folder for ASP.Net applications using IIS5.0 where it
works for both the ASPNet machine account and for a different account if i
configure the ASP.Net process to run as an alternative account by altering
the machine.config file. It gives me back
C:\Documents and Settings\myuser\Application Data where i'm using a local
user...
|
by: Stuart Shay |
last post by:
Hello All
I have a ASP.NET 2.0 checkbox list, the problem is when 0 items are checked
I am unable to determine if the list is empty
//Determine Checked Roles
CheckBoxList ckUserRoleList =
gvUserList.Rows.FindControl("ckUserRoleList") as CheckBoxList;
StringBuilder oStringBuilder = new StringBuilder();
| |
by: biganthony via AccessMonster.com |
last post by:
Hi,
I have the following code to select a folder and then delete it. I keep
getting a Path/File error on the line that deletes the actual folder. The
line before that line deletes the files in the folder but I get the error
message when the procedure attempts to delete the folder selected in the
BrowseforFolderbyPath function.
How can I get it to delete the folder that the user has selected in the
Browse for Folder dialog?
|
by: paktsardines |
last post by:
Dear all,
As of yesterday I have this function:
char ** lines = read_file("filename.txt");
Now, I want to print the contents of lines. A first attempt:
int i=0;
|
by: Larry Bates |
last post by:
I have a need to implement a drop folder upload mechanism for secure uploading
of files to a server. At first glance this appears that it would be an easy
application to write. Then I begin to think about the race conditions that
exist between the process that will wake up to upload the files and the fact
that the user can add additional files to the drop folder at any point in time.
I would like to clear out files/folders after they have...
|
by: fniles |
last post by:
I am using VB.NET 2005.
When I try to delete a folder that has files underneath it, it gave me "the
directory is not empty" error.
f = New IO.DirectoryInfo("C:\myfolder")
If f.Exists Then
f.Delete() --error "the directory is not empty"
End If
How can I delete a folder with files underneath it ? Thank you.
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |