根据文件名PYTHON创建子目录和排序文件

时间:2016-02-12 18:52:42

标签: python sorting recursion shutil

我有一个包含许多零件文件及其修订版的大型目录,我想以递归方式为每个零件创建一个新文件夹,然后将所有相关文件移动到该文件夹​​中。我试图通过隔离一个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)

这是我到目前为止所做的,基本上我不知道如何获取原始文件名并将其用作其余文件的匹配模式。我想用于此匹配模式的原始文件名只是一个7位数字,所有相关文件可能都有其他字符(例如REV-2)。

1 个答案:

答案 0 :(得分:2)

Don't overthink it

I think you're getting confused about what HTTP.call(method,route,{ headers:{ "Content-Type":"application/json" } }) gives you - recheck the docs. os.walk() and dirs are just a list of names of the directories / files, not the full paths.

Here's my suggestion. Assuming that you're starting with a directory layout something like:

files

And want to end with something like:

directory1
    1234567abc.txt
1234567abc.txt
1234567bcd.txt
2234567abc.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:

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

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 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) to fpath. This is because it's not a path but just the name of the file, so it's better to call it fname.

Please clarify the question if that's not what you meant!

[edit] to show how to copy the file without changing its name.