我有一个包含多个文件(.pdf,.doc,.zip)的平面文件夹,这些文件需要根据文本文件的输入而移动/分类到现有的分层文件夹中。文本文件格式如下。
{fold_location}〜{XXXX}〜{0001} {file_id}或12345678〜ABCD〜0000 FileName
我需要使用fold_location并转到新文件夹树中的该文件夹,然后将文件从平面文件夹复制到fold_location文件夹中。有没有简单的方法可以做到这一点,或者我必须使用搜索树数据结构。
我已经研究出如何从文本文件中提取Fold_location和文件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.