read list of dirnames and search for filenames

Discussion in 'Python' started by fscked, May 1, 2007.

  1. fscked

    fscked Guest

    I cannot seem to get this to work. I am hyst trying to read in a list
    of paths and see if the directory or any sub has a filename pattern.
    Here is the code:

    import os, sys
    from path import path

    myfile = open("boxids.txt", "r")
    for line in myfile.readlines():
    d = path(line)
    for f in d.walkfiles('*Config*.xml'):
    print f

    And here is my error:

    Traceback (most recent call last):
    File "Untitled.py", line 21, in ?
    for f in d.walkfiles('*Config*.xml'):
    File "C:\Python24\Lib\site-packages\path.py", line 460, in walkfiles
    childList = self.listdir()
    File "C:\Python24\Lib\site-packages\path.py", line 328, in listdir
    names = os.listdir(self)
    WindowsError: [Errno 3] The system cannot find the path specified: u'X:
    \\Instructions\\97544546294\n/*.*'

    What I don't get is if I just print the path it prints correctly, but
    it keeps adding double "\"s to it.

    I tried changing the backslashies to forward slashies and I get :
    WindowsError: [Errno 3] The system cannot find the path specified:
    u'X:/Instructions/97544546294\n/*.*'

    help?
    fscked, May 1, 2007
    #1
    1. Advertising

  2. fscked

    Rob Wolfe Guest

    fscked <> writes:

    > I cannot seem to get this to work. I am hyst trying to read in a list
    > of paths and see if the directory or any sub has a filename pattern.
    > Here is the code:
    >
    > import os, sys
    > from path import path
    >
    > myfile = open("boxids.txt", "r")
    > for line in myfile.readlines():


    Instead of this:

    > d = path(line)


    try this:

    d = path(line.strip())

    ``readlines`` doesn't remove trailing newline characters from string

    > for f in d.walkfiles('*Config*.xml'):
    > print f
    >
    > And here is my error:
    >
    > Traceback (most recent call last):
    > File "Untitled.py", line 21, in ?
    > for f in d.walkfiles('*Config*.xml'):
    > File "C:\Python24\Lib\site-packages\path.py", line 460, in walkfiles
    > childList = self.listdir()
    > File "C:\Python24\Lib\site-packages\path.py", line 328, in listdir
    > names = os.listdir(self)
    > WindowsError: [Errno 3] The system cannot find the path specified: u'X:
    > \\Instructions\\97544546294\n/*.*'
    >


    --
    HTH,
    Rob
    Rob Wolfe, May 1, 2007
    #2
    1. Advertising

  3. fscked

    Rob Wolfe Guest

    Rob Wolfe <> writes:

    > fscked <> writes:
    >
    >> I cannot seem to get this to work. I am hyst trying to read in a list
    >> of paths and see if the directory or any sub has a filename pattern.
    >> Here is the code:
    >>
    >> import os, sys
    >> from path import path
    >>
    >> myfile = open("boxids.txt", "r")
    >> for line in myfile.readlines():


    And you don't need to use ``readlines`` at all.
    This is enough:

    for line in myfile:

    --
    HTH,
    Rob
    Rob Wolfe, May 1, 2007
    #3
  4. fscked

    fscked Guest

    On May 1, 2:36 pm, Rob Wolfe <> wrote:
    > Rob Wolfe <> writes:
    > > fscked <> writes:

    >
    > >> I cannot seem to get this to work. I am hyst trying to read in a list
    > >> of paths and see if the directory or any sub has a filename pattern.
    > >> Here is the code:

    >
    > >> import os, sys
    > >> from path import path

    >
    > >> myfile = open("boxids.txt", "r")
    > >> for line in myfile.readlines():

    >
    > And you don't need to use ``readlines`` at all.
    > This is enough:
    >
    > for line in myfile:
    >
    > --
    > HTH,
    > Rob


    Worked well, thanks!
    fscked, May 1, 2007
    #4
    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. Replies:
    2
    Views:
    19,178
  2. Edison Carter
    Replies:
    1
    Views:
    448
    brucie
    Sep 30, 2003
  3. B.J.
    Replies:
    4
    Views:
    725
    Toby Inkster
    Apr 23, 2005
  4. Abby Lee
    Replies:
    5
    Views:
    375
    Abby Lee
    Aug 2, 2004
  5. Replies:
    5
    Views:
    154
    Dr.Ruud
    Jun 7, 2006
Loading...

Share This Page