python - 如何检查目录大小/如何确保重新定位文件完成
问题描述
我有一个脚本,它遍历rootdir
有另一个文件夹的监视文件夹。每个文件夹可以包含:movie.mp4, trailer.mp4, xmlfile.xml, picture.jpg, subtitles1.dfxp, subtitles2.dfxp
.
然后脚本开始评估是否可以将文件打包到存档中 - 要求是找到:movie.mp4, picture.jpg, xmlfile.xml
. 其余文件是可选的。
我遇到了一个问题:
- 假设我们有一个
testfolder
要移动到的文件夹rootdir
。 testfolder
包含文件movie.mp4, trailer.mp4, subtitles1.dfxp, picture.jpg, xmlfile.xml
:.testfolder
被拖放到中rootdir
。- 在移动
testfolder
目录的过程中,已经进行了打包评估。 - 文件
movie.mp4, picture.jpg, xmlfile.xml
已传输,但trailer.mp4
仍在subtitles1.dfxp
传输过程中。 - 脚本开始打包
movie.mp4, picture.jpg, xmlfile.xml
,因为打包的要求得到满足。和被省略subtitles1.dfxp
,trailer.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))
解决方案
推荐阅读
- html - CSS Flexbox:如何为所有人设置相同的高度
- 独立于不同字体大小的项目?
- css - 如何在自动调整网格列模板中使用一列占用 2 个跨度将网格项包装为全宽
- php - Symfony5; 无法访问连接的实体值
- django - 用于在管理页面中列出 aws iam 用户的 django 模型
- html - Contact Form Submit Button link to Email
- java - 处理器线程、Tomcat 线程、Java 线程
- flutter - 具有不同笔划宽度的 CircularProgressIndicator
- r - 在数据框中查找以分钟为单位的时间差并添加为列
- javascript - 关于如何基于“功能”(nestjs)构建项目和路线的建议
- c++ - 返回数组时出现分段错误