By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,621 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,621 IT Pros & Developers. It's quick & easy.

read list of dirnames and search for filenames

P: n/a
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?

May 1 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
fscked <fs*********@gmail.comwrites:
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
May 1 '07 #2

P: n/a
Rob Wolfe <rw@smsnet.plwrites:
fscked <fs*********@gmail.comwrites:
>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
May 1 '07 #3

P: n/a
On May 1, 2:36 pm, Rob Wolfe <r...@smsnet.plwrote:
Rob Wolfe <r...@smsnet.plwrites:
fscked <fsckedag...@gmail.comwrites:
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!

May 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.