在我的elif not块中,我需要检查文本文件的下一行,因为代码会继续检查相同的文件ID并卡在此部分中。文本文件提供了位于单独文件夹(FilesFolder)中的文件的所需位置和名称。这些文件需要移至现有的空树中,并位于正确的子目录中。
import os
import os.path
import shutil
from pathlib import Path
data_folder = Path("C:/Users/Folder")
FilesFolder = Path("C:/Users/Files")
file_to_open = data_folder / "text.txt"
RootFolder = "TopFolder"
NewFolderTree = data_folder / RootFolder
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:
# Iterating through files that need to be sorted.
for file in os.listdir(FilesFolder):
# Extracting the name, ignoring revision,extension(.zip,.pdf etc) and dashes.
file_name = file.split('_')[0].split('-')[0].split('.')[0]
for file_id in f:
if file_name == file_id:
# Check if the folder exists then copy the file to the corresponding folder in new tree.
for root, dirs, subdirs in os.walk(NewFolderTree):
if fold_location.exists():
dest = Path(fold_location)
# copy files occurs here
shutil.copy(file, dest)
elif not subdir.exists():
print("Folder does not exist for " + fold_location)
elif not file_name == file_id:
# how do I check the next line from text file?
# gets to end of text file.
else:
print('The following document is missing ' + file_name )