473,378 Members | 1,543 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,378 software developers and data experts.

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 1863
"Pl********@gmail.com" <Pl********@gmail.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********@gmail.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****@tundraware.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
by: calca77 | last post by:
Hi, Please, could anyone tell me how to check if a directory exists. Thank you everyone. D.
2
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...
5
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...
3
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
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...
2
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
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...
2
by: CCLeasing | last post by:
If (Directory.Exists(@"c:\indigo\" + txtOfficial + @"\" + comOffice.Text + @"\" + numTerm.Value.ToString())) { storereport(); } else { Directory.CreateDirectory(@"c:\indigo\" + txtOfficial +...
1
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
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.