Os.walk wont work with directory acquired through

2019-08-31 09:05发布

I'm using os.walk to search for files in specific directories.

This is testcode that wont do what it should:

import os, re

cwd = os.getcwd() 
directory= 'Box II'
dirpattern = re.compile(r'^.*?\\'+directory+'.*?', re.M)

for root, dirs, files in os.walk(os.path.abspath(cwd)):

    if dirpattern.search(root):
        match = dirpattern.search(root)
        match = match.group(0).encode('string-escape')

print match '''OUTPUT = D:\\dir1\\dir2\\dir3'''

for roots, dirss, filess in os.walk(match):
print filess '''OUPUT = gives nothing'''

if I type the dirname out in the second loop like this

  for roots, dirss, filess in os.walk('D:\\dir1\\dir2\\dir3'):
      print filess

I get the right output.

The dirnames have whitespaces.

What I type is exactly what is printed for match out of the first loop. Why doesn't it work?

Is the match.group(0) not a string?

If I do this:

import os, re

cwd = os.getcwd() 
directory= 'Box II'
dirpattern = re.compile(r'^.*?\\'+directory+'.*?', re.M)

for root, dirs, files in os.walk(os.path.abspath(cwd)):

    if dirpattern.search(root):
        match = dirpattern.search(root)
        match = match.group(0)

and use this function:

escape_dict={'\\':r'\\'}

def raw(text):
"""Returns a raw string representation of text"""
 return "".join([escape_dict.get(char,char) for char in text])

match1= raw(match)
print match '''OUTPUT = D:\dir1\dir2\dir3 '''
print match1 '''OUTPUT = D:\\dir1\\dir2\\dir3''' 

for roots, dirss, filess in os.walk('match1'):

    print filess '''OUTPUT= nothing'''

os.walk doesnt work either but when I do this:

match= 'D:\dir1\dir2\dir3'
match1= raw(match)
print match1 '''OUTPUT= D:\\dir1\\dir2\\dir3'''

os.walk works:

for roots, dirss, filess in os.walk('match1'):
    print filess '''OUTPUT= [file1,file2,file3]'''

What's the difference between match acquired from a regex

match = match.group(0)
print match '''OUTPUT = D:\dir1\dir2\dir3 '''

and match just written out as a string

match = 'D:\dir1\dir2\dir3'

1条回答
趁早两清
2楼-- · 2019-08-31 09:54

You should probably remove encode('string-escape') from your code.

查看更多
登录 后发表回答