lists and files question

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

    lists and files question

    This code:

    import os, re, string
    setpath = raw_input("Ente r the path: ")
    for root, dirs, files in os.walk(setpath ):
    id = re.compile('Mic rosoft Excel Worksheet')
    fname = files
    # print fname
    content = open(fname[1],'rb')

    Produces this error:

    IOError: Error[2] No such file or directory 'Name of File'

    The strange thing is that it correctly identifies the file that it says
    doesn't exist. Could someone explain why this is?

    Also, is "files" a nested list? It looks like one, but I'm not entirely
    sure as I'm still relatively new to Python. Thanks!

  • Mark Day

    #2
    Re: lists and files question

    In article <3F1DCF52.70406 07@hotmail.com> , hokiegal99
    <hokiegal99@hot mail.com> wrote:
    [color=blue]
    > This code:
    >
    > import os, re, string
    > setpath = raw_input("Ente r the path: ")
    > for root, dirs, files in os.walk(setpath ):
    > id = re.compile('Mic rosoft Excel Worksheet')
    > fname = files
    > # print fname
    > content = open(fname[1],'rb')
    >
    > Produces this error:
    >
    > IOError: Error[2] No such file or directory 'Name of File'
    >
    > The strange thing is that it correctly identifies the file that it says
    > doesn't exist. Could someone explain why this is?[/color]

    The problem is that file doesn't exist in the current working
    directory; it's in another directory (stored in "root" in your code).

    Try this:
    content = open(os.path.jo in(root,fname[1]), 'rb')
    [color=blue]
    > Also, is "files" a nested list? It looks like one, but I'm not entirely
    > sure as I'm still relatively new to Python. Thanks![/color]

    It is a list of strings. Each string is the name of one of the files
    in the directory (whose path is in "root" above).

    -Mark

    Comment

    • Sean 'Shaleh' Perry

      #3
      Re: lists and files question

      On Tuesday 22 July 2003 16:57, hokiegal99 wrote:[color=blue]
      > This code:
      >
      > import os, re, string
      > setpath = raw_input("Ente r the path: ")
      > for root, dirs, files in os.walk(setpath ):
      > id = re.compile('Mic rosoft Excel Worksheet')
      > fname = files
      > # print fname
      > content = open(fname[1],'rb')
      >
      > Produces this error:
      >
      > IOError: Error[2] No such file or directory 'Name of File'
      >[/color]

      if you replace your logic with some prints you will quickly see the problem.

      What happens is os.walk() passes filenames without their path. You need to
      use os.path.join() to get the full name back.


      Comment

      • hokiegal99

        #4
        Re: lists and files question


        "print fname" prints out the list of files in "setpath" w/o problem. How
        does it do that if os.walk doesn't give it the path to the files?

        Here's some output from "print fname":

        ['index.txt', 'CELL-MINUTES.xls', '.nautilus-metafile.xml']
        ['2000 Schedule.xls', '2001 State.pdf', '2001.pdf', 'A Little More Like
        Bill.doc', 'AARP.doc', "Accounting 's of Dad's Est.xls",
        'Amortization_T able.xls', 'huey letter.doc', 'utt-R&D.pdf', 'utt.pdf',
        'rest.xls', 'Debts and Assets.xls', 'First Accounting - Estate.xls',
        'Friends.doc', "Grace's.do c", 'Home Address.doc', 'Ins.doc',
        'Insurance.doc' , 'Interro.doc', 'Marshall.doc', 'NBB home loan.doc',
        'Position Description.doc ', "andy's", "Andy's Travel Voucher.xls",
        "Andy's Travel.xls", 'Rebuttal.doc', 'Refinance.doc' , 'TaxReturn 2.pdf',
        'TaxReturn 3.pdf', 'TaxReturn 4.pdf', 'TaxReturn 5.pdf',
        'TaxReturn.pdf' , 'The Casey Song.doc', "Touch of the Hand.xls", 'Workout
        Sheet.xls', 'application.pd f', 'budsum.pdf']

        When I add os.path.join like this:

        setpath = raw_input("Ente r the path: ")
        for root, dirs, files in os.walk(setpath ):
        id = re.compile('Mic rosoft Excel Worksheet')
        fname = files
        print fname
        content = open(os.path.jo in(root,fname[0]),'rb')

        I get a "IndexError : list index out of range" error.

        This is a Linux 2.4 computer running Python 2.3b2... if that matters.

        Thanks!

        Sean 'Shaleh' Perry wrote:[color=blue]
        > On Tuesday 22 July 2003 16:57, hokiegal99 wrote:
        >[color=green]
        >>This code:
        >>
        >>import os, re, string
        >>setpath = raw_input("Ente r the path: ")
        >>for root, dirs, files in os.walk(setpath ):
        >> id = re.compile('Mic rosoft Excel Worksheet')
        >> fname = files
        >># print fname
        >> content = open(fname[1],'rb')
        >>
        >>Produces this error:
        >>
        >>IOError: Error[2] No such file or directory 'Name of File'
        >>[/color]
        >
        >
        > if you replace your logic with some prints you will quickly see the problem.
        >
        > What happens is os.walk() passes filenames without their path. You need to
        > use os.path.join() to get the full name back.[/color]




        Comment

        • hokieghal99

          #5
          Re: lists and files question

          Thanks to everyone for the feedback on this. I've learned a lot from you
          guys.

          John Hunter wrote:[color=blue]
          > Others have already answered your question - I just want to point out
          > a few of other things
          >
          > import os, re, string
          > setpath = raw_input("Ente r the path: ")
          > for root, dirs, files in os.walk(setpath ):
          > id = re.compile('Mic rosoft Excel Worksheet')
          >
          > 1) id is a built in function; you may not want to override it with
          > your variable name
          >[color=green][color=darkred]
          > >>> x = 1
          > >>> id(x)[/color][/color]
          > 135313208
          >
          > 2) The reason to use re.compile is for efficiency. There is no need
          > to call it inside the loop, since you're just recompiling the same
          > regex over and over again. Instead, compile the regex outside the
          > loop
          >[color=green][color=darkred]
          > >>> rgx = re.compile('[A-Z]+')
          > >>> for some_text in some_list:[/color][/color]
          > ... m = rgx.match(some_ text)
          >
          > 3) If you want to match 'Microsoft Excel Worksheet', you don't need
          > regular expressions since this is a string literal. You will
          > probably be better off just using the string find method, as in
          >
          > s.find('Microso ft Excel Worksheet')
          >
          > 4) You may want to look at the path module, which provides a nice
          > interface for walking over files:
          > http://www.jorendorff.com/articles/python/path/
          >[color=green][color=darkred]
          > >>> from path import path
          > >>> xldir = path(setpath)
          > >>> for f in xldir.files('*. xls'):[/color][/color]
          > ... print f.read().find(' Microsoft Excel Worksheet')
          >
          > Cheers,
          > John Hunter
          >[/color]

          Comment

          Working...