Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-使用模式匹配目录(正则表达式)_Python_Regex_Python 2.7 - Fatal编程技术网

Python-使用模式匹配目录(正则表达式)

Python-使用模式匹配目录(正则表达式),python,regex,python-2.7,Python,Regex,Python 2.7,我编写了一个循环,它忽略了其中包含.txt文件的所有子目录 src = raw_input("Enter source disk location: ") src = os.path.abspath(src) dst = raw_input("Enter first destination to copy: ") dst = os.path.abspath(dst) dest = raw_input("Enter second destination to move : ") dest = os

我编写了一个循环,它忽略了其中包含.txt文件的所有子目录

src = raw_input("Enter source disk location: ")
src = os.path.abspath(src)
dst = raw_input("Enter first destination to copy: ")
dst = os.path.abspath(dst)
dest = raw_input("Enter second destination to move : ")
dest = os.path.abspath(dest) 

path_patter = '(\S+)_(\d+)_(\d+)_(\d+)__(\d+)_(\d+)_(\d+)'

for dir, dirs, files in os.walk(src):
if any(f.endswith('.txt') for f in files):
    dirs[:] = []  # do not recurse into subdirectories
    continue  
files = [os.path.join(dir, f) for f in files ]

for f in files:

    part1 = os.path.dirname(f)
    part2 = os.path.dirname(os.path.dirname(part1))
    part3 = os.path.split(part1)[1]
    path_miss1 = os.path.join(dst, "missing_txt")
    path_miss = os.path.join(path_miss1, part3)
    path_missing = os.path.join(dest, "missing_txt")
    searchFileName = re.search(path_patter, part3)#### update

    if searchFileName:#####update
    try:
        if not os.path.exists(path_miss):
            os.makedirs(path_miss)
        else:
            pass

        if os.path.exists(path_miss):
            distutils.dir_util.copy_tree(part1, path_miss)
        else:
            debug_status += "missing_file\n"
            pass

        if (get_size(path_miss)) == 0:
            os.rmdir(path_miss)
        else:
            pass

        if not os.path.exists(path_missing):
            os.makedirs(path_missing)
        else:
            pass

        if os.path.exists(path_missing):
            shutil.move(part1, path_missing)
        else:
            pass

        if (get_size(path_missing)) == 0:
            os.rmdir(path_missing)
        else:
            pass
    except Exception:
        pass
    else:
    continue
在本例中,如何修改此代码以将目录名与正则表达式进行比较。(它必须忽略包含.txt文件的目录)

我在这里做了几件事,希望这个例子能有所帮助

  • 我为WindowsFS编写了这篇文章,因此使用了两个路径转换函数
  • 此脚本会忽略带有.txt文件的dirs,就像您实现它一样
  • 此脚本将从启动脚本的目录开始,并且仅在模式匹配时打印文件名。对于上一条规则未忽略的所有子目录,都会执行此操作
  • 在python中使用正则表达式,并使其针对每个目录再次编译,因此您会得到:'directory/(\S+)(\d+)(\d+)\uuud+)(\d+)(\d+)'

我想这还不起作用。还是这样?如果粘贴的脚本中没有选项卡,则files变量将为空。您还没有将目录或文件名与模式进行比较。我做了其他事情,使用os.path.split(part1)[1]将路径分割为字符串,然后通过re.search将其与模式进行比较,结果成功了:)但多亏了您,我有了一个简单的想法;)
import os
import re

def createEscapedPattern(path,pattern):
    newPath = os.path.normpath(path)
    newPath = newPath.replace("\\","\\\\\\\\")
    return newPath + "\\\\\\\\" +  pattern

def createEscapedPath(path):
    newPath =  os.path.normpath(path)
    return newPath.replace("\\","\\\\")

src = 'C:\\Home\\test'
path_patter = '(\S+)_(\d+)_(\d+)_(\d+)__(\d+)_(\d+)_(\d+)$'
p = re.compile(createEscapedPattern(src,path_patter))
for dir, dirs, files in os.walk(src):
    if any(f.endswith('.txt') for f in files):
        dirs[:] = []
        continue
    if any(p.match(createEscapedPath(dir)) for f in files):
        for f in files:
            print createEscapedPath(dir + "/" + f)
    p = re.compile(createEscapedPattern(dir,path_patter))