473,587 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I check if the listdir result a directory?

I have the following code which list out all the files/directories
under the current directory.

But how can I find out if 'f' is a directory?

for f in os.listdir(".") :
// how can I check if 'f' a directory?

Mar 23 '07 #1
2 1872
"Pl********@gma il.com" <Pl********@gma il.comwrites:
for f in os.listdir(".") :
// how can I check if 'f' a directory?
Your two approaches are 1) use os.stat (or stat.stat) and check the
mode bits; or 2) use os.listdir and catch the OSError exception you
get if you use it on a non-directory.
Mar 23 '07 #2
Pl********@gmai l.com wrote:
I have the following code which list out all the files/directories
under the current directory.

But how can I find out if 'f' is a directory?

for f in os.listdir(".") :
// how can I check if 'f' a directory?
Try os.path.isdir()

--
----------------------------------------------------------------------------
Tim Daneliuk tu****@tundrawa re.com
PGP Key: http://www.tundraware.com/PGP/
Mar 23 '07 #3

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

Similar topics

3
16493
by: calca77 | last post by:
Hi, Please, could anyone tell me how to check if a directory exists. Thank you everyone. D.
2
460
by: A.J.M. van Rijthoven | last post by:
I want to open a form that is based on a query. When the output of the query results in 0 records, I don't want to open the form but just display a messagebox. Is there a way to check if the result of the query = 0 records??
5
15764
by: Marcia Hon | last post by:
Hi, I would like to know how the following may be accomplished in C: 1. Check if a directory is empty. 2. Empty the directory. Please if you know how could you state what statements in C would accomplish this? Thanks,
3
25509
by: Bill nguyen | last post by:
Is there a quick way to determine if a directory is empty. Directory.Getfiles() doesn't tell me how many files without having to loop thru the list. Thanks Bill
4
29554
by: yinglcs | last post by:
I check the documentation here, but it does not say how to check if a directory is exist in python? http://docs.python.org/lib/os-file-dir.html And why mkdir fails if the directory already exists? Thank you.
2
4781
by: Joris De Groote | last post by:
Hi How can I check if a directory exists in a network drive? I can check it on the local machine, but when I want to check a network drive, that doenst work, how can I do this? Thanks Joris
1
1642
by: Udi | last post by:
Hi, I need to copy several files as a post build step of the Test project, to the 'Out' directory of a running test. (The current directory of a running test) Is there any simple way of knowing in the build process what will be the output directory? Thanks
2
3945
by: CCLeasing | last post by:
If (Directory.Exists(@"c:\indigo\" + txtOfficial + @"\" + comOffice.Text + @"\" + numTerm.Value.ToString())) { storereport(); } else { Directory.CreateDirectory(@"c:\indigo\" + txtOfficial + @"\" + comOffice.Text + @"\" + numTerm.Value.ToString());
1
2557
by: aine_canby | last post by:
Hi, How do I check if a directory exists within another directory? For example C:/aaa C:/Aaa = true C:/aaa/ C:/Aaa/bb = true C:/aaa/ C:/Aaa/bb/cc = true C:/bb/ C:/aa/ = false
0
7920
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...
0
7849
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...
0
8215
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. ...
1
7973
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...
0
6626
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
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...
1
2358
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
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1189
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...

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.