Os.walk wont work with directory acquired through ' match.group(0).encode('string-escape') '

[亡魂溺海] 提交于 2019-12-11 19:33:01

问题


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:


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



来源:https://stackoverflow.com/questions/10934199/os-walk-wont-work-with-directory-acquired-through-match-group0-encodestrin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!