472,995 Members | 1,882 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,995 software developers and data experts.

os.path.isfile()

Here is a program to print out the files in a directory:

-----------
import os

myfiles = os.listdir("../")
print myfiles

for afile in myfiles:
print afile
if os.path.isfile(afile):
print afile, "___file"
if os.path.isdir(afile):
print afile, "___dir"
print
------------

Here is what's in the directory:

----------
$ ls -al ../

total 2576
drwxr-xr-x 8 nnn nnn 272 Jul 1 03:03 .
drwxr-xr-x 25 nnn nnn 850 Jul 1 01:34 ..
-rw-r--r-- 1 nnn nnn 6148 Jul 1 03:02 .DS_Store
-rw-r--r-- 1 nnn nnn 1300000 Jun 27 14:02 aaa.txt
drwxr-xr-x 55 nnn nnn 1870 Jul 1 03:09 dir1
-rwxrwxrwx 1 nnn nnn 263 Jun 27 22:40 mytest.py
-rw-r--r-- 1 nnn nnn 0 Mar 4 16:15 scratch.txt
-rw-r--r-- 1 nnn nnn 275 Apr 11 03:40 xmlFile.xml
------------

Here is the output from my program:

----------
$ python 1test.py

['.DS_Store', 'aaa.txt', 'dir1', 'mytest.py', 'scratch.txt',
'xmlFile.xml']
..DS_Store
..DS_Store ___file

aaa.txt
aaa.txt ___file

dir1

mytest.py

scratch.txt

xmlFile.xml

$
--------------

I expected the output:

-----------
DS_Store
..DS_Store ___file

aaa.txt
aaa.txt ___file

dir1
dir1 ___dir

mytest.py
mytest.py ___file

scratch.txt
scratch.txt ___file

xmlFile.xml
xmlFile.xml ___file
------------

Jul 1 '07 #1
2 4659
7stud <bb**********@yahoo.comwrote:
Here is a program to print out the files in a directory:

-----------
import os

myfiles = os.listdir("../")
print myfiles

for afile in myfiles:
print afile
if os.path.isfile(afile):
print afile, "___file"
if os.path.isdir(afile):
print afile, "___dir"
print
------------

Here is what's in the directory:

----------
$ ls -al ../

total 2576
drwxr-xr-x 8 nnn nnn 272 Jul 1 03:03 .
drwxr-xr-x 25 nnn nnn 850 Jul 1 01:34 ..
-rw-r--r-- 1 nnn nnn 6148 Jul 1 03:02 .DS_Store
-rw-r--r-- 1 nnn nnn 1300000 Jun 27 14:02 aaa.txt
drwxr-xr-x 55 nnn nnn 1870 Jul 1 03:09 dir1
-rwxrwxrwx 1 nnn nnn 263 Jun 27 22:40 mytest.py
-rw-r--r-- 1 nnn nnn 0 Mar 4 16:15 scratch.txt
-rw-r--r-- 1 nnn nnn 275 Apr 11 03:40 xmlFile.xml
------------
Yes, but what does 'ls -al .' show you? You didn't put any kind of path in
your calls to isfile/isdir, so you are checking for the existence of
files/directories called '.DS_Store' etc. in the *current* directory, not
the one above. From your output I'd guess you have .DS_Store and aaa.txt
files but the other names are not duplicated.

Jul 1 '07 #2
On Jul 1, 3:36 am, Duncan Booth <duncan.bo...@invalid.invalidwrote:
7stud <bbxx789_0...@yahoo.comwrote:
Here is a program to print out the files in a directory:
-----------
import os
myfiles = os.listdir("../")
print myfiles
for afile in myfiles:
print afile
if os.path.isfile(afile):
print afile, "___file"
if os.path.isdir(afile):
print afile, "___dir"
print
------------
Here is what's in the directory:
----------
$ ls -al ../
total 2576
drwxr-xr-x 8 nnn nnn 272 Jul 1 03:03 .
drwxr-xr-x 25 nnn nnn 850 Jul 1 01:34 ..
-rw-r--r-- 1 nnn nnn 6148 Jul 1 03:02 .DS_Store
-rw-r--r-- 1 nnn nnn 1300000 Jun 27 14:02 aaa.txt
drwxr-xr-x 55 nnn nnn 1870 Jul 1 03:09 dir1
-rwxrwxrwx 1 nnn nnn 263 Jun 27 22:40 mytest.py
-rw-r--r-- 1 nnn nnn 0 Mar 4 16:15 scratch.txt
-rw-r--r-- 1 nnn nnn 275 Apr 11 03:40 xmlFile.xml
------------

Yes, but what does 'ls -al .' show you? You didn't put any kind of path in
your calls to isfile/isdir, so you are checking for the existence of
files/directories called '.DS_Store' etc. in the *current* directory, not
the one above. From your output I'd guess you have .DS_Store and aaa.txt
files but the other names are not duplicated.
Thanks.

Jul 1 '07 #3

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

Similar topics

6
by: kimes | last post by:
I've just started digging into how python works.. I found that other mudules are clearly declared like one file per a module.. But the only os.path doesn't have their own file.. ye I know is...
70
by: Michael Hoffman | last post by:
Many of you are familiar with Jason Orendorff's path module <http://www.jorendorff.com/articles/python/path/>, which is frequently recommended here on c.l.p. I submitted an RFE to add it to the...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
5
by: spike grobstein | last post by:
So, I've got this project I'm working on where the app defines various classes that are subclassed by module packages that act like plugins... I'd like the packages to define a file path for...
6
by: NickP | last post by:
Hi there, The following returns "False"... MsgBox(New Uri("ftp://me.pop.com/pop.bmp").IsFile.ToString) Is there any particular reason? It seems like a pretty poor function to me as that is...
10
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. ...
5
by: 7stud | last post by:
Here's the code: ------------ import os, os.path, pprint mydir = "/Users/me/2testing" files = pprint.pprint(files) print os.path.join(mydir, "helloWorld.py")
10
by: ppaterson | last post by:
Can os.path.isfile(x) ever return True after os.remove(x) has successfully completed? (Windows 2003, Python 2.3) We had a couple of failures in a server application that we cannot yet reproduce...
0
by: Gabriel Genellina | last post by:
En Wed, 27 Aug 2008 01:15:58 -0300, aditya shukla <adityashukla1983@gmail.comescribi�: Can't you get the program name and path from the place you got it originally, before adding it to the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.