read list of dirnames and search for filenames

F

fscked

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?
 
R

Rob Wolfe

fscked said:
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/*.*'
 
R

Rob Wolfe

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

for line in myfile:
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top