简体   繁体   English

创建基于文件名PYTHON的子目录并对文件进行排序

[英]Creating subdirectories and sorting files based on filename PYTHON

I have a large directory with many part files with their revisions, I want to recursively create a new folder for each part, and then move all of the related files into that folder. 我的目录很大,其中包含许多带有修订版本的零件文件,我想为每个零件递归创建一个新文件夹,然后将所有相关文件移动到该文件夹​​中。 I am trying to do this by isolating a 7 digit number which would be used as an identifier for the part, and all the related filenames would also include this number. 我试图通过隔离一个7位数的数字来做到这一点,该数字将用作部件的标识符,并且所有相关的文件名也将包括该数字。

import os
import shutil
import csv
import glob
from fnmatch import fnmatch, filter
from os.path import isdir, join
from shutil import copytree, copy2, Error, copystat
from shutil import copytree, ignore_patterns


dirname = ' '

# pattern =  '*???????*'

for root, dirs, files in os.walk(dirname):
    for fpath in files:
        print(fpath)
        if fpath[0:6].isdigit():
            matchdir = os.mkdir(os.path.join(os.path.dirname(fpath)))
            partnum = str(fpath[0:6])
            pattern = str(partnum)
            filematch = fnmatch(files, pattern)
            print(filematch)
            shutil.move(filematch, matchdir)

This is what I have so far, basically I'm not sure how to get the original filename and use it as the matching patter for the rest of the files. 到目前为止,这就是我所拥有的,基本上我不确定如何获取原始文件名并将其用作其余文件的匹配模式。 The original filename I want to use for this matching pattern is just a 7 digit number, and all of the related files may have other characters (REV-2) for example. 我想用于此匹配模式的原始文件名只是7位数字,并且所有相关文件都可能带有其他字符(例如REV-2)。

Don't overthink it 不要想太多

I think you're getting confused about what os.walk() gives you - recheck the docs . 我认为os.walk()给您带来的困惑让您感到困惑-重新检查文档 dirs and files are just a list of names of the directories / files, not the full paths. dirsfiles只是目录/文件名称的列表,而不是完整路径。

Here's my suggestion. 这是我的建议。 Assuming that you're starting with a directory layout something like: 假设您从目录布局开始,如下所示:

directory1
    1234567abc.txt
1234567abc.txt
1234567bcd.txt
2234567abc.txt
not-interesting.txt

And want to end with something like: 并希望以类似以下内容结束:

directory1
    1234567
        abc.txt
1234567
    abc.txt
    bcd.txt
2234567
    abc.txt
not-interesting.txt

If that's correct, then there's no need to rematch the files in the directory, just operate on each file individually, and make the part directory only if it doesn't already exist. 如果是正确的话,则无需重新匹配目录中的文件,只需对每个文件进行单独操作,并仅在不存在零件目录的情况下创建零件目录。 I would also use a regular expression to do this, so something like: 我还将使用正则表达式来执行此操作,例如:

import os
import re
import shutil

for root, dirs, files in os.walk(dirname):
    for fname in files:
        # Match a string starting with 7 digits followed by everything else.
        # Capture each part in a group so we can access them later.
        match_object = re.match('([0-9]{7})(.*)$', fname)
        if match_object is None:
            # The regular expression did not match, ignore the file.
            continue

        # Form the new directory path using the number from the regular expression and the current root.
        new_dir = os.path.join(root, match_object.group(1))
        if not os.path.isdir(new_dir):
            os.mkdir(new_dir)

        new_file_path = os.path.join(new_dir, match_object.group(2))

        # Or, if you don't want to change the filename, use:
        new_file_path = os.path.join(new_dir, fname)

        old_file_path = os.path.join(root, fname)
        shutil.move(old_file_path, new_file_path)

Note that I have: 请注意,我有:

  • Switched the sense of the condition, we continue the loop immediately if the file is not interesting. 切换条件后,如果文件不有趣,我们将立即继续循环。 This is a useful pattern to use to make sure that your code does not get too heavily indented. 这是一种有用的模式,可用于确保代码不会缩进过多。
  • Changed the name of fpath to fname . fpath的名称更改为fname This is because it's not a path but just the name of the file, so it's better to call it fname . 这是因为它不是路径,而只是文件名,因此最好将其命名为fname

Please clarify the question if that's not what you meant! 如果这不是您的意思,请说明问题!

[edit] to show how to copy the file without changing its name. [edit]显示如何在不更改文件名的情况下复制文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM