os.path.isfile() error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • 7stud

    os.path.isfile() error

    Here's the code:
    ------------
    import os, os.path, pprint

    mydir = "/Users/me/2testing"

    files = [file for file in os.listdir(mydi r)]
    pprint.pprint(f iles)

    print os.path.join(my dir, "helloWorld.py" )

    files = [file
    for file in os.listdir(mydi r)
    if os.path.isfile( os.path.join(di r, file) )
    ]

    pprint.pprint(f iles)
    ----output:----------------

    ['.DS_Store', 'cpTest', 'dir1', 'testfile1', 'xmlFile.xml']
    /Users/me/2testing/helloWorld.py
    Traceback (most recent call last):
    File "test1.py", line 16, in ?
    files = [file
    File "/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
    python2.3/posixpath.py", line 62, in join
    elif path == '' or path.endswith('/'):
    AttributeError: 'builtin_functi on_or_method' object has no attribute
    'endswith'

  • Peter Otten

    #2
    Re: os.path.isfile( ) error

    7stud wrote:
    Here's the code:
    ------------
    import os, os.path, pprint
    >
    mydir = "/Users/me/2testing"
    >
    files = [file for file in os.listdir(mydi r)]
    pprint.pprint(f iles)
    >
    print os.path.join(my dir, "helloWorld.py" )
    >
    files = [file
    for file in os.listdir(mydi r)
    if os.path.isfile( os.path.join(di r, file) )
    That should be mydir, not dir.
    ]
    >
    pprint.pprint(f iles)
    ----output:----------------
    >
    ['.DS_Store', 'cpTest', 'dir1', 'testfile1', 'xmlFile.xml']
    /Users/me/2testing/helloWorld.py
    Traceback (most recent call last):
    File "test1.py", line 16, in ?
    files = [file
    File "/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
    python2.3/posixpath.py", line 62, in join
    elif path == '' or path.endswith('/'):
    AttributeError: 'builtin_functi on_or_method' object has no attribute
    'endswith'

    Comment

    • 7stud

      #3
      Re: os.path.isfile( ) error

      On Apr 7, 2:56 am, "7stud" <bbxx789_0...@y ahoo.comwrote:
      Here's the code:
      ------------
      import os, os.path, pprint
      >
      mydir = "/Users/me/2testing"
      >
      files = [file for file in os.listdir(mydi r)]
      pprint.pprint(f iles)
      >
      print os.path.join(my dir, "helloWorld.py" )
      >
      files = [file
      for file in os.listdir(mydi r)
      if os.path.isfile( os.path.join(di r, file) )
      ]
      >
      pprint.pprint(f iles)
      ----output:----------------
      >
      ['.DS_Store', 'cpTest', 'dir1', 'testfile1', 'xmlFile.xml']
      /Users/me/2testing/helloWorld.py
      Traceback (most recent call last):
      File "test1.py", line 16, in ?
      files = [file
      File "/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
      python2.3/posixpath.py", line 62, in join
      elif path == '' or path.endswith('/'):
      AttributeError: 'builtin_functi on_or_method' object has no attribute
      'endswith'
      I got it: 'mydir' v. 'dir'
      mydir = "/Users/me/2testing"
      ...
      if os.path.isfile( os.path.join(di r, file) )

      Comment

      • John Machin

        #4
        Re: os.path.isfile( ) error

        On Apr 7, 6:56 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
        Here's the code:
        ------------
        import os, os.path, pprint
        >
        mydir = "/Users/me/2testing"
        >
        files = [file for file in os.listdir(mydi r)]
        pprint.pprint(f iles)
        >
        print os.path.join(my dir, "helloWorld.py" )
        >
        files = [file
        for file in os.listdir(mydi r)
        if os.path.isfile( os.path.join(di r, file) )
        ]
        >
        pprint.pprint(f iles)
        ----output:----------------
        >
        ['.DS_Store', 'cpTest', 'dir1', 'testfile1', 'xmlFile.xml']
        /Users/me/2testing/helloWorld.py
        Traceback (most recent call last):
        File "test1.py", line 16, in ?
        files = [file
        File "/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
        python2.3/posixpath.py", line 62, in join
        elif path == '' or path.endswith('/'):
        AttributeError: 'builtin_functi on_or_method' object has no attribute
        'endswith'
        Re your subject: os.path.isfile is nothing to do with the problem.
        As is evident from the traceback, the error (which is in *your* code)
        was detected in os.path.join, before os.path.isfile was called.

        Looking at the source file (posixpath.py), one sees that the offending
        "path" (the builtin function/method with no endswith attribute) is the
        first formal arg of os.path.join. You have supplied "dir" as the first
        actual arg. dir is a builtin function. You meant "mydir" instead.

        BTW, don't use "file" (or the name of any other builtin function) for
        your own variables. Use meaningful names -- what you have called
        "file" is not a file, it is the name of a file. Get pychecker and/or
        pylint and run them over your source periodically. They'll detect not
        only shadowing builtins but many other types of actual and potential
        gotchas.

        HTH,
        John

        Comment

        • mik3l3374@gmail.com

          #5
          Re: os.path.isfile( ) error

          On Apr 7, 4:56 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
          Here's the code:
          ------------
          import os, os.path, pprint
          >
          mydir = "/Users/me/2testing"
          >
          files = [file for file in os.listdir(mydi r)]
          pprint.pprint(f iles)
          >
          print os.path.join(my dir, "helloWorld.py" )
          >
          files = [file
          for file in os.listdir(mydi r)
          if os.path.isfile( os.path.join(di r, file) )
          ]
          >
          pprint.pprint(f iles)
          ----output:----------------
          >
          ['.DS_Store', 'cpTest', 'dir1', 'testfile1', 'xmlFile.xml']
          /Users/me/2testing/helloWorld.py
          Traceback (most recent call last):
          File "test1.py", line 16, in ?
          files = [file
          File "/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
          python2.3/posixpath.py", line 62, in join
          elif path == '' or path.endswith('/'):
          AttributeError: 'builtin_functi on_or_method' object has no attribute
          'endswith'
          is 'dir' defined? or is it 'mydir'?

          Comment

          • Steve Holden

            #6
            Re: os.path.isfile( ) error

            mik3l3374@gmail .com wrote:
            On Apr 7, 4:56 pm, "7stud" <bbxx789_0...@y ahoo.comwrote:
            >Here's the code:
            >------------
            >import os, os.path, pprint
            >>
            >mydir = "/Users/me/2testing"
            >>
            >files = [file for file in os.listdir(mydi r)]
            >pprint.pprint( files)
            >>
            >print os.path.join(my dir, "helloWorld.py" )
            >>
            >files = [file
            >for file in os.listdir(mydi r)
            >if os.path.isfile( os.path.join(di r, file) )
            >]
            >>
            >pprint.pprint( files)
            >----output:----------------
            >>
            >['.DS_Store', 'cpTest', 'dir1', 'testfile1', 'xmlFile.xml']
            >/Users/me/2testing/helloWorld.py
            >Traceback (most recent call last):
            > File "test1.py", line 16, in ?
            > files = [file
            > File "/System/Library/Frameworks/Python.framewor k/Versions/2.3/lib/
            >python2.3/posixpath.py", line 62, in join
            > elif path == '' or path.endswith('/'):
            >AttributeError : 'builtin_functi on_or_method' object has no attribute
            >'endswith'
            >
            is 'dir' defined? or is it 'mydir'?
            >
            [this is for 7stud, not for the poster] Clearly it should be 'mydir',
            but 'dir' is also defined - it's a built-in function, which is why it
            has no 'endswith' method.

            regards
            Steve

            --
            Steve Holden +44 150 684 7255 +1 800 494 3119
            Holden Web LLC/Ltd http://www.holdenweb.com
            Skype: holdenweb http://del.icio.us/steve.holden
            Recent Ramblings http://holdenweb.blogspot.com

            Comment

            Working...