使用 python 复制具有完整文件夹结构的文件
Copy files using python with complete folder structure
几天后我要换一个更好的 SSD,我有一堆数据存储在上面,如果删除我可能会后悔。我唯一需要的文件类型是 PDF 文件、docx 文件、txt 文件和其他文件。
因此,我编写了一个脚本来使用 python.
定位这些文件
# to copy all of my documents into another location.
import sys
import os
import time
import pathlib
import json
filePath=["D:\", "C:\Users"]
# ext=['mkv','docx','doc','pdf','mp4','zip',]
fileExt=["**\*.docx","**\*.doc","**\*.pdf"]
fileList={}
for each_drive in filePath:
fileList[each_drive]={}
for each_type in fileExt:
fileList[each_drive][each_type]=list(pathlib.Path(each_drive).glob(each_type))
file1 = open('test.txt', 'w')
for each in fileList.values():
for each2 in each.values():
for entry in each2:
print(entry)
file1.writelines(str(str(entry)+ "\n"))
file1.close()
此脚本仅定位格式与 FileExt 列表匹配的文件,并将这些位置写出到 test.txt 文件。
现在我需要传输这些文件,同时保持准确的目录结构。
例如,如果有一个文件为
C:\Users\<MyUser>\AppData\Local\Files\S0\Attachments\hpe[4].docx
脚本应将整个目录结构复制为
<BackupDrive>:\<BackupFolderName>\C\Users\<MyUser>\AppData\Local\Files\S0\Attachments\hpe[4].docx
如何使用这个确切的结构进行复制。
TLDR:需要复制文件,同时保持使用 Python
的目录结构
P.S。我正在使用 Windows,Python 3.8
既然您能够将数据写入文件,我假设您也知道如何从该文件读取数据。然后对于每一行(比如在该文件中调用它 source
,使用 shutil.copyfile(source, dest)
.
您可以通过操作 source
:
创建 dest
字符串
# remove 'C:'
str_split = source[2:]
# add backup drive and folder
dest = ''.join(['<BackupDrive>:\<BackupFolderName>', str_split])
如评论中所述,目标路径不会自动创建,但可以像此处解释的那样处理:create destination path for shutil.copy files
对于文件列表中的每一行,执行以下操作:
for filePath in fileList:
destination = .join(['<BackupDrive>:\<BackupFolderName>', filePath[2:]])
os.makedirs(os.path.dirname(filePath), exist_ok=True)
shutil.copy(filePath , destination)
感谢@Emmo 和@FloLie 的回答。我只需要为列表中的每个文件使用 os.makedirs() 函数并将 exist_ok 标志设置为 true。
这是紧跟在问题代码之后的代码。
#######################################
# create destination directory
file1=open ('test.txt', 'r')
text= file1.readlines()
# print(text)
for each in text:
each=each[:-1]
destination="BackupDIR-"+each[0]+each[2:]
os.makedirs(os.path.dirname(destination), exist_ok=True)
shutil.copy(each,destination)
这使得整个代码看起来像:
# to copy all of my documents into another location.
import os
import time
import pathlib
import json
import shutil
filePath=["D:\", "C:\Users"]
# ext=['mkv','docx','doc','pdf','mp4','zip',]
fileExt=["**\*.docx","**\*.doc","**\*.pdf"]
fileList={}
for each_drive in filePath:
fileList[each_drive]={}
for each_type in fileExt:
fileList[each_drive][each_type]=list(pathlib.Path(each_drive).glob(each_type))
file1 = open('test.txt', 'w')
for each in fileList.values():
for each2 in each.values():
for entry in each2:
print(entry)
file1.writelines(str(str(entry)+ "\n"))
file1.close()
#######################################
# create destination directory
file1=open ('test.txt', 'r')
text= file1.readlines()
# print(text)
for each in text:
each=each[:-1]
destination="BackupDIR-"+each[0]+each[2:]
os.makedirs(os.path.dirname(destination), exist_ok=True)
shutil.copy(each,destination)
P.S。这个答案只适用于像我这样有时无法脱离上下文理解小片段的人
几天后我要换一个更好的 SSD,我有一堆数据存储在上面,如果删除我可能会后悔。我唯一需要的文件类型是 PDF 文件、docx 文件、txt 文件和其他文件。 因此,我编写了一个脚本来使用 python.
定位这些文件# to copy all of my documents into another location.
import sys
import os
import time
import pathlib
import json
filePath=["D:\", "C:\Users"]
# ext=['mkv','docx','doc','pdf','mp4','zip',]
fileExt=["**\*.docx","**\*.doc","**\*.pdf"]
fileList={}
for each_drive in filePath:
fileList[each_drive]={}
for each_type in fileExt:
fileList[each_drive][each_type]=list(pathlib.Path(each_drive).glob(each_type))
file1 = open('test.txt', 'w')
for each in fileList.values():
for each2 in each.values():
for entry in each2:
print(entry)
file1.writelines(str(str(entry)+ "\n"))
file1.close()
此脚本仅定位格式与 FileExt 列表匹配的文件,并将这些位置写出到 test.txt 文件。 现在我需要传输这些文件,同时保持准确的目录结构。 例如,如果有一个文件为
C:\Users\<MyUser>\AppData\Local\Files\S0\Attachments\hpe[4].docx
脚本应将整个目录结构复制为
<BackupDrive>:\<BackupFolderName>\C\Users\<MyUser>\AppData\Local\Files\S0\Attachments\hpe[4].docx
如何使用这个确切的结构进行复制。
TLDR:需要复制文件,同时保持使用 Python
的目录结构
P.S。我正在使用 Windows,Python 3.8
既然您能够将数据写入文件,我假设您也知道如何从该文件读取数据。然后对于每一行(比如在该文件中调用它 source
,使用 shutil.copyfile(source, dest)
.
您可以通过操作 source
:
dest
字符串
# remove 'C:'
str_split = source[2:]
# add backup drive and folder
dest = ''.join(['<BackupDrive>:\<BackupFolderName>', str_split])
如评论中所述,目标路径不会自动创建,但可以像此处解释的那样处理:create destination path for shutil.copy files
对于文件列表中的每一行,执行以下操作:
for filePath in fileList:
destination = .join(['<BackupDrive>:\<BackupFolderName>', filePath[2:]])
os.makedirs(os.path.dirname(filePath), exist_ok=True)
shutil.copy(filePath , destination)
感谢@Emmo 和@FloLie 的回答。我只需要为列表中的每个文件使用 os.makedirs() 函数并将 exist_ok 标志设置为 true。
这是紧跟在问题代码之后的代码。
#######################################
# create destination directory
file1=open ('test.txt', 'r')
text= file1.readlines()
# print(text)
for each in text:
each=each[:-1]
destination="BackupDIR-"+each[0]+each[2:]
os.makedirs(os.path.dirname(destination), exist_ok=True)
shutil.copy(each,destination)
这使得整个代码看起来像:
# to copy all of my documents into another location.
import os
import time
import pathlib
import json
import shutil
filePath=["D:\", "C:\Users"]
# ext=['mkv','docx','doc','pdf','mp4','zip',]
fileExt=["**\*.docx","**\*.doc","**\*.pdf"]
fileList={}
for each_drive in filePath:
fileList[each_drive]={}
for each_type in fileExt:
fileList[each_drive][each_type]=list(pathlib.Path(each_drive).glob(each_type))
file1 = open('test.txt', 'w')
for each in fileList.values():
for each2 in each.values():
for entry in each2:
print(entry)
file1.writelines(str(str(entry)+ "\n"))
file1.close()
#######################################
# create destination directory
file1=open ('test.txt', 'r')
text= file1.readlines()
# print(text)
for each in text:
each=each[:-1]
destination="BackupDIR-"+each[0]+each[2:]
os.makedirs(os.path.dirname(destination), exist_ok=True)
shutil.copy(each,destination)
P.S。这个答案只适用于像我这样有时无法脱离上下文理解小片段的人