os.path.isfile()

Discussion in 'Python' started by 7stud, Jul 1, 2007.

  1. 7stud

    7stud Guest

    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
    ------------
     
    7stud, Jul 1, 2007
    #1
    1. Advertising

  2. 7stud

    Duncan Booth Guest

    7stud <> wrote:

    > 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.
     
    Duncan Booth, Jul 1, 2007
    #2
    1. Advertising

  3. 7stud

    7stud Guest

    On Jul 1, 3:36 am, Duncan Booth <> wrote:
    > 7stud <> wrote:
    > > 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.
     
    7stud, Jul 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gary Wessle

    os.isfile() error

    Gary Wessle, May 6, 2006, in forum: Python
    Replies:
    1
    Views:
    434
    Adonis
    May 6, 2006
  2. Steve Holden

    Re: os.path.isfile with *.tar.gz

    Steve Holden, Mar 15, 2007, in forum: Python
    Replies:
    1
    Views:
    345
    Bruno Desthuilliers
    Mar 15, 2007
  3. 7stud

    os.path.isfile() error

    7stud, Apr 7, 2007, in forum: Python
    Replies:
    5
    Views:
    427
    Steve Holden
    Apr 7, 2007
  4. Replies:
    10
    Views:
    537
    Paul Paterson
    Jun 6, 2007
  5. Replies:
    1
    Views:
    433
    Peter Otten
    Dec 30, 2010
Loading...

Share This Page