簡體   English   中英

Python-使用模式匹配目錄(正則表達式)

[英]Python - match directories with pattern (regular expression)

我寫了一個循環,忽略了其中包含.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文件的目錄)

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))

我在這里做了幾件事,希望這個例子對您有所幫助

  • 我為Windows fs編寫了此代碼,因此使用了兩個路徑轉換功能。
  • 該腳本會像您實現它一樣忽略帶有.txt文件的目錄
  • 該腳本將從您啟動腳本的目錄開始,並且僅在模式匹配時才打印文件名。 對於先前規則未忽略的所有子目錄,將完成此操作。
  • 在python中使用了正則表達式,並使其針對每個目錄再次進行編譯,因此您得到:'directory /(\\ S +) (\\ d +) (\\ d +)_(\\ d +)__(\\ d +) (\\ d +) (\\ d +)$ '

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM