473,766 Members | 2,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Determining whether it's a file or a directory?

os.listdir("pat h") returns the names of all the items in a directory ok,
but I need to know whether the item is a file or a directory.

Can anybody tell me how this could be done?
Jul 18 '05 #1
3 1688
Dfenestr8 wrote:
os.listdir("pat h") returns the names of all the items in a directory ok,
but I need to know whether the item is a file or a directory.

Can anybody tell me how this could be done?


The standard way would be to perform

os.path.isdir(o s.path.join(pat h, dir_or_file))

on the item in question, but
import os
for path, dirs, files in os.walk("path") : .... break
.... dirs ['only', 'directories'] files ['some', 'files']


gives you directories and files in separate lists.

Peter

Jul 18 '05 #2
Dfenestr8 wrote:
os.listdir("pa th") returns the names of all the items in a directory ok,
but I need to know whether the item is a file or a directory.

Can anybody tell me how this could be done?

os.path.isdir(" path")

Aaron

Jul 18 '05 #3
Dfenestr8 wrote:
os.listdir("pat h") returns the names of all the items in a directory ok,
but I need to know whether the item is a file or a directory.

Can anybody tell me how this could be done?

Some others have shown you how to test if a file is a directory, as
well as how to get a separated list of files and directories, but
I'd like to point out a mistake you are making: you believe a name
is associated with either a file or a directory, but not both. There
are already a few file systems where this is not true, and the trend
seems to be towards, rather than away from this.

The os.walk technique would automatically cope with this situation
(the same name could show up in both lists). Using the os.path.isdir
technique, you are more likely to decide that "if something is a
directory, it is not a file (ie has no non-directory data)."

-Scott David Daniels
Sc***********@A cm.Org
Jul 18 '05 #4

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

Similar topics

12
3665
by: Cliff Wells | last post by:
Hi, I'm writing an application that needs to know if an Internet connection is available. Basically, I want to have something similar to what a lot of email clients have, where the app can work either in "online" or "offline" mode (it keeps a cache of downloaded info, so it can work without a connection if needed). The basic problem is this: it downloads info (RSS feeds) from a variety of sources. Any one (or more) of these could...
2
7898
by: Phil Galey | last post by:
Using the following, you can determine the size of a file: Dim fi As New IO.FileInfo(<Path to file>) MsgBox(fi.Length) .... but what about the size of a directory? The IO.DirectoryInfo object doesn't have a Length property. In is there a way in VB.NET to determine the size of a directory without having to resort to importing the Scripting DLL and using the FileSystemObject? Thanks.
4
2161
by: petermichaux | last post by:
Hi, I'm hoping for a reason I'm wrong or an alternate solution... I'd like to be able to dynamically include some javascript files. This is like scriptaculous.js library but their solution is broken in Firefox 1.5.0.1 on OS X. What happens with the Scriptaculous library is this In the html document the author only has to include one line
2
2079
by: pob | last post by:
I was all happy because thru some tutelage of others I created this array to find the newest file in a directory. However, I then realized its great that I now know the latest file, but I need to know the name of the file. Unfortunately, I am only passing the date into the array. Do I need to create a m-d array ? If so how do I create a 2-d array where I do not know how many rows I will be looping thru ?
1
1170
by: topher | last post by:
When using filesystemwatcher to keep an eye on a directory to see if there are any files, how will I know when it is safe to work on the files in a directory? In other words, how will I know that no more files are being written and that the directory is static? -topher
1
2562
by: topramen | last post by:
does any one here know of a good way to to determine whether or not a given path is a directory (e.g., "c:\mydir") or a file (e.g., "c:\mydir \myfile.txt")? i started attacking this problem by using filesystemobject to check whether or not a directory existed for the path. if so, it was a directory path. if not, i checked to see if a file existed for it. if so, it was a file path. this code is shown below: ...
6
1573
by: Ben | last post by:
Hello, I hope this is the correct group for this posting :-) For the following kind of code fragment: ofstream filestream; filestream.open(path); if(!filestream.is_open()) { cerr<< ......
4
2119
by: Nathan Sokalski | last post by:
When determining whether a String can be converted to a DateTime, you can use the IsDate() method. However, I would also like to know whether the string is a date, a time, or both a date and a time. Is there any simple way to do this without using manual pattern matching? Thanks. -- Nathan Sokalski njsokalski@hotmail.com http://www.nathansokalski.com/
13
10722
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to process the file. The appearance of the command file triggers the device to grab the original file. My problem is I don't want to write the command file to the device until the original file from the workflow has been copied completely. Since these...
0
9404
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
10008
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
9959
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
8833
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5279
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...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
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
3
2806
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.