首页 > 解决方案 > 如何检查目录大小/如何确保重新定位文件完成

问题描述

我有一个脚本,它遍历rootdir有另一个文件夹的监视文件夹。每个文件夹可以包含:movie.mp4, trailer.mp4, xmlfile.xml, picture.jpg, subtitles1.dfxp, subtitles2.dfxp.

然后脚本开始评估是否可以将文件打包到存档中 - 要求是找到:movie.mp4, picture.jpg, xmlfile.xml. 其余文件是可选的。

我遇到了一个问题:

  1. 假设我们有一个testfolder要移动到的文件夹rootdir
  2. testfolder包含文件movie.mp4, trailer.mp4, subtitles1.dfxp, picture.jpg, xmlfile.xml:.
  3. testfolder被拖放到中rootdir
  4. 在移动testfolder目录的过程中,已经进行了打包评估。
  5. 文件movie.mp4, picture.jpg, xmlfile.xml已传输,但trailer.mp4仍在subtitles1.dfxp传输过程中。
  6. 脚本开始打包movie.mp4, picture.jpg, xmlfile.xml,因为打包的要求得到满足。和被省略subtitles1.dfxptrailer.mp4因为它们没有被计入需要打包的文件中。

我怎样才能防止这种情况?如何确保没有其他可选文件等待完全传输?

我的想法是在评估要打包的文件之前检查文件夹的大小。我想检查文件夹的当前大小,等待一秒钟,然后再次检查大小。如果大小保持不变,则表示文件传输完成。然而,另一个问题发生了。要计算文件夹大小,需要文件夹中每个文件的大小总和。无法直接检查文件夹大小。此外,在单个文件传输期间,在传输期间检查文件大小时(例如,我创建了一个循环,每秒检查目录中文件的大小,然后拖放大文件),它已经给出了最终大小。所有这一切让我得出结论,无法检查文件夹是否正在传输或是否已经完成传输。

你知道确保所有文件都被传输的任何可能的解决方案吗?

while True:
    for dirpath, dirnames, filenames in os.walk(rootdir):
        # Object FolderInfo contains objects 'FileInfo'
        folder = FolderInfo()
        folder.folderPath = dirpath
        obj = os.scandir(path=dirpath)
        for entry in obj: 
            if entry.is_file():
                # Object FileInfo contains information about path, filename, type (movie,trailer etc)
                file = FileInfo(entry.name)
                file.get_file_information(rootdir, dirpath)
                folder.append_file(file)
        # If at least 3 files were found, assessment for packing is performed.
        if len(folder.listOfFiles)>=3:
            # Verify checks if movie+image+xmlfile was found, if so it sets folder.packingFlag to True
            folder.verify()
        if folder.packingFlag == True:
            # Folder containing information of files paths that are going to be packed is passed to foldersReadyToPack list
            self.foldersReadyToPack.append(folder)
        obj.close()    
    if len(self.foldersReadyToPack) > 0:
        # After traversing through all directories in rootdir packingQueue is created, it contains folders of list foldersReadyToPack
        packingQueue = PackingQueue(self.foldersReadyToPack)
        # Here the packing is performed
        packingQueue.execute_packing()

time.sleep(int(self.scanTime))

标签: pythondirectoryoperating-systemsizefilesize

解决方案


推荐阅读