Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > read list of dirnames and search for filenames

Reply
Thread Tools

read list of dirnames and search for filenames

 
 
fscked
Guest
Posts: n/a
 
      05-01-2007
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?

 
Reply With Quote
 
 
 
 
Rob Wolfe
Guest
Posts: n/a
 
      05-01-2007
fscked <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Rob Wolfe
Guest
Posts: n/a
 
      05-01-2007
Rob Wolfe <(E-Mail Removed)> writes:

> fscked <(E-Mail Removed)> 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
 
Reply With Quote
 
fscked
Guest
Posts: n/a
 
      05-01-2007
On May 1, 2:36 pm, Rob Wolfe <(E-Mail Removed)> wrote:
> Rob Wolfe <(E-Mail Removed)> writes:
> > fscked <(E-Mail Removed)> 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!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to read Chinese filenames? ckyang74@gmail.com Perl Misc 5 06-06-2006 11:00 PM
problem with filenames, Filenames and FILENAMES B.J. HTML 4 04-23-2005 08:13 PM
Read Filenames from C:\Folder rbutch@coair.com ASP .Net 2 01-21-2005 07:57 PM
Why can some browsers read filenames with spaces. Edison Carter HTML 1 09-30-2003 07:10 AM



Advertisments