os.path.isfile()

7stud bbxx789_05ss at yahoo.com
Sun Jul 1 05:18:02 EDT 2007


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
------------




More information about the Python-list mailing list