使用Python创建多层字母文件夹结构

2024-06-16 13:51:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着写一个简短的脚本,把几千个文件按字母顺序排列成文件夹,深度任意多个。这是为了允许在旧硬件(例如Amiga 500)上浏览树,而不需要任何给定文件夹中的文件数量锁定UI。在

例如,“算盘”文件将进入:

/A/AB/ABA/算盘

“三角形”在:

/T/TR/TRI/三角形

我想将深度指定为一个变量,这样我就可以尝试不同数量的级别。三个可能太少或太多。在

我正在为创建初始文件夹结构所需的递归而努力。我可以获得源文件的名称和位置,创建新的文件夹,并使用操作系统库(分别是walk、mkdir和,路径.目录名/路径.join),已经解决了如何将文件复制到各自正确的位置,以及如何在复制后删除未使用的文件夹(例如,'/C/CF/CFZ'路径不太可能以其中的任何文件,假设所有文件名都是英文单词)。但在第一步我会绕圈子。在

任何帮助都将不胜感激。在

敬上。在


Tags: 文件路径脚本文件夹ui数量硬件ab
3条回答

下面是一个更优雅的版本:

import os
import itertools

depth = 3

#create the base directory
basepath = 'alphabet'
os.mkdir(basepath)

#setup alphabet list
#alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alphabet = 'ABC'   #short alphabet for testing purposes

current_path = [basepath]

def create_hierarchy(max_depth, current_path, current_depth=0):
    if current_depth < max_depth:
        for i in alphabet:
            newfolder = (''.join(current_path[-1]) + i) if current_depth != 0 else i
            os.mkdir(os.path.join(*current_path, newfolder))
            create_hierarchy(max_depth, current_path + [newfolder], current_depth + 1)
    else:
        return

create_hierarchy(depth, current_path)

它改编自用户3235916的答案。在

在itertools.组合(iterable,r)可能会有帮助

我只是展示前5个字母,不同的r
从combinations()输出生成目录名应该很容易
(它返回一个生成器,需要打印中的*号)

import string
from itertools import combinations

print(*combinations(string.ascii_uppercase[0:5], 2), sep='\n')
('A', 'B')
('A', 'C')
('A', 'D')
('A', 'E')
('B', 'C')
('B', 'D')
('B', 'E')
('C', 'D')
('C', 'E')
('D', 'E')

print(*combinations(string.ascii_uppercase[0:5], 3), sep='\n')
('A', 'B', 'C')
('A', 'B', 'D')
('A', 'B', 'E')
('A', 'C', 'D')
('A', 'C', 'E')
('A', 'D', 'E')
('B', 'C', 'D')
('B', 'C', 'E')
('B', 'D', 'E')
('C', 'D', 'E')

print(*combinations(string.ascii_uppercase[0:5], 4), sep='\n')
('A', 'B', 'C', 'D')
('A', 'B', 'C', 'E')
('A', 'B', 'D', 'E')
('A', 'C', 'D', 'E')
('B', 'C', 'D', 'E')

可能有一种更优雅的方法来实现这一点,但这应该是有效的:

import os
import itertools

depth = 3

#create the base directory
basepath = '/path/to/alphabet'
os.mkdir(basepath)

#setup alphabet list
#long to crate full directory structure
#alphabet = 'A B C D E F G H I J K L M N O P Q R S T U V W X Y Z'  
alphabet = 'A B C'   #short alphabet for testing purposes
alphabet = alphabet.split()

#helper variable for determining what depth you are at
c_depth = alphabet

#create depth 0
for letter in alphabet:
    #print letter
    pth = os.path.join(basepath,letter)
    os.mkdir(pth)

#
for d in range(depth-1):
    #take the cartesian product of your current directories and your alphabet
    #in order to obtain all possible permutations. Will return tuples like
    #('AAA','B')
    result = list(itertools.product(*(alphabet,c_depth)))
    #print d

    #reset the current directory list
    cdirs = []

    #for each directory at the new depth
    for elem in result:

        #create the full name by joining tuples created previously
        dName =  ''.join(itertools.chain(elem))

        #build up path where it belongs from the letters it is comprised of
        pth = basepath
        for l in range(len(dName)-1):
            pth = os.path.join(pth,dName[:l+1])

        #add the new directory folder to the end of the path
        pth = os.path.join(pth,dName)

        #make new directory
        os.mkdir(pth)
        #print dName
        cdirs.append(dName)

    #reset helper variable
    c_depth = cdirs

相关问题 更多 >