如何将文件从平面文件夹移动到给定位置的分层文件夹

2024-04-26 04:48:41 发布

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

我有一个单一的平面文件夹,其中包含许多文件(.pdf、.doc、.zip)。这些文件需要根据文本文件的输入移动/排序到现有的分层文件夹中。文本文件格式如下所示

{fold_location}{XXXX}{0001}{file_id}或12345678~ABCD~0000文件名

我需要使用折叠位置并转到新文件夹树中的文件夹,然后将文件从平面文件夹复制到折叠位置文件夹。有没有一种简单的方法可以做到这一点,或者我必须使用搜索树数据结构

我已经解决了如何从文本文件中提取折叠位置和文件ID。这是我唯一需要的文本文件

import os
import os.path

from pathlib import Path
data_folder = Path("C:/Users/Me/pack")
data_folder2 = Path("C:/Users/Me/pack/PACK")
file_to_open = data_folder / "TextFile.txt"
FilesFolder = data_folder2 / Files
NewFolderTree = data_folder / RootNewFolder

with open(file_to_open) as f:
#Striping the whitespace and extracting important parts.
line = f.readline().strip()
fold_location = line.split("~")[0]
file_id = line.split(" ")[1]
   while line:
    for subdir, dirs in os.walk(NewFolderTree):
        if folder_location.exists(NewFolderTree): 
                          # get file_id from FilesFolder and copy it in to correct folder location in NewFolderTree
                        if file_id is not in FilesFolder:
                          #  make text file with text "Missing" in folder location.

Tags: 文件topathinimport文件夹iddata