我遇到以下问题(我在macOS上): 12个USB闪存驱动器安装在/ Volumes中,它们的名称从cam0到cam11。每个驱动器都具有以下结构:cam0 / DCIM / 100HDDVR / C00_0001.mp4,cam1 / DCIM / 100HDDVR / C01_0001.mp4等。在每个100HDDVR文件夹中将有多个文件,因此对于cam0,例如它将是:C00_0001。 mp4,C00_0002.mp4,C00_0003.mp4等 现在我想把它复制到桌面上,比方说,创建一个新的文件夹叫做:录制[添加今天日期]然后创建一个子文件夹shoot1,它将包含文件C00_0001.mp4到C11_0001.mp4并创建子文件夹shoot2将包含C00_0002.mp4到C11_0002.mp4,依此类推,直到复制闪存驱动器中的所有文件。
到目前为止,我设法将所有文件从cam0 / DCIM / 100HDDVR复制到新的文件夹录音/ cam0,但它不够自动化,我正在努力更新它。
def copyy(self):
root.update_idletasks()
self.status['text'] = "Files are being copyied, have patience ;)".format(self.status)
self.source_direcotry0= '/Volumes/CAM0/DCIM/100HDDVR'
self.source_direcotry1= '/Volumes/CAM1/DCIM/100HDDVR'
self.source_direcotry2= '/Volumes/CAM2/DCIM/100HDDVR'
self.source_direcotry3= '/Volumes/CAM3/DCIM/100HDDVR'
self.source_direcotry4= '/Volumes/CAM4/DCIM/100HDDVR'
self.source_direcotry5= '/Volumes/CAM5/DCIM/100HDDVR'
self.source_direcotry6= '/Volumes/CAM6/DCIM/100HDDVR'
self.source_direcotry7= '/Volumes/CAM7/DCIM/100HDDVR'
self.source_direcotry8= '/Volumes/CAM8/DCIM/100HDDVR'
self.source_direcotry9= '/Volumes/CAM9/DCIM/100HDDVR'
self.source_direcotry10= '/Volumes/CAM10/DCIM/100HDDVR'
self.source_direcotry11= '/Volumes/CAM11/DCIM/100HDDVR'
self.path0="recording/CAM0"
self.path1="recording/CAM1"
self.path2="recording/CAM2"
self.path3="recording/CAM3"
self.path4="recording/CAM4"
self.path5="recording/CAM5"
self.path6="recording/CAM6"
self.path7="recording/CAM7"
self.path8="recording/CAM8"
self.path9="recording/CAM9"
self.path10="recording/CAM10"
self.path11="recording/CAM11"
self.cam0=os.path.join(self.Destination.get(), self.path0)
self.cam1=os.path.join(self.Destination.get(), self.path1)
self.cam2=os.path.join(self.Destination.get(), self.path2)
self.cam3=os.path.join(self.Destination.get(), self.path3)
self.cam4=os.path.join(self.Destination.get(), self.path4)
self.cam5=os.path.join(self.Destination.get(), self.path5)
self.cam6=os.path.join(self.Destination.get(), self.path6)
self.cam7=os.path.join(self.Destination.get(), self.path7)
self.cam8=os.path.join(self.Destination.get(), self.path8)
self.cam9=os.path.join(self.Destination.get(), self.path9)
self.cam10=os.path.join(self.Destination.get(), self.path10)
self.cam11=os.path.join(self.Destination.get(), self.path11)
self.p.start(1)
self.work_thread = threading.Thread(target=self.copyy2, args=())
self.work_thread.start()
self.work_thread.join()
self.p.stop()
self.status['text'] = "Files have been copyied".format(self.status)
def copyy2(self):
shutil.copytree(self.source_direcotry0, self.cam0)
shutil.copytree(self.source_direcotry1, self.cam1)
shutil.copytree(self.source_direcotry2, self.cam2)
shutil.copytree(self.source_direcotry3, self.cam3)
shutil.copytree(self.source_direcotry4, self.cam4)
shutil.copytree(self.source_direcotry5, self.cam5)
shutil.copytree(self.source_direcotry6, self.cam6)
shutil.copytree(self.source_direcotry7, self.cam7)
shutil.copytree(self.source_direcotry8, self.cam8)
shutil.copytree(self.source_direcotry9, self.cam9)
shutil.copytree(self.source_direcotry10, self.cam10)
shutil.copytree(self.source_direcotry11, self.cam11)
除此之外,你将如何处理这个问题,因此它也适用于Windows,当安装闪存驱动器时,你看不到它们的名字和磁盘字母总是不同。这甚至可能吗?
我希望我能够清楚地描述它,并提前感谢任何提示。
答案 0 :(得分:0)
我会采取另一种方式:
对于(1)你可以使用:
def get_filepaths(directory, extension=None):
"""
Generate the file names in a directory
tree by walking the tree either top-down or bottom-up. For each
directory in the tree rooted at directory top (including top itself),
it yields a 3-tuple (dirpath, dirnames, filenames).
"""
import os, ntpath
assert(os.path.isdir(directory)), "The 'directory' parameter is not a directory (%s)"%(directory)
file_paths = [] # List which will store all of the full filepaths.
# Walk the tree.
for root, directories, files in os.walk(directory):
for filename in files:
# Join the two strings in order to form the full filepath.
filepath = os.path.join(root, filename)
if not ntpath.basename(filepath).startswith("."):
file_paths.append(filepath) # Add it to the list.
if extension:return [x for x in file_paths if x.endswith(extension) ]
return file_paths # Self-explanatory
对于(2)和(3),你可以在循环中使用相同的功能:
# this is a pseudo code - no tested and has to be customized
nb_shoot = n
for shoot_n in range(nb_shoot):
# 1) looking for files to copy
nb_zeros = 3 - len(str(shoot_n)) # calculating the number of 0 to put in front of the filename
zeros = "0"*nb_zeros
extension = "%s%shoot_n.mp4"%(zeros, shoot_n)
paths_for_the_shoot = get_filepaths("/Volumes/", extension)
#2) creating a new dir
current_date = time.time()
new_dir_name = "recording_%s/shoot_%s"%(current_date , shoot_n )
os.mkdir(new_dir_name)
for path in paths_for_the_shoot:
#copy file
os.copy(path, new_dir_name)