转换 C++进入Python代码,循环遍历目录并从文件中获取时间戳?
我的任务是将 C++ 脚本转换为 python 脚本。该脚本的目的是循环遍历 mediaDB 中的所有目录(按开始和结束日期)并计算 zip 文件的大小。我一直坚持让 for 循环遍历目录,它在 python 中与 C++ 中非常不同,我在 C++ 方面有更多经验。有人可以提供任何建议吗?
C++ 代码
// This will loop over each core files directory and sum the file size.
directory_iterator dirIt(mediaDBCoreFilesDir);
for (directory_iterator dirIt(mediaDBCoreFilesDir);dirIt!=directory_iterator();dirIt++)
另外,如果有人对如何从 python 文件中获取最后更新时间戳有任何想法,我们将非常感激。 C++ 代码是:
// Get the last update timestamp from the file
std::time_t t = last_write_time(*dirIt);
ptime fileTimeStamp = from_time_t(t);
编辑:我试图首先编写一个 for 语句,循环遍历所有目录并总结它们的文件大小。我不需要编辑、删除或打印任何目录,只需获取文件大小。那么使用 os.walk 和 os.path.getsize 是否更合适?
其次,我需要从文件中检索最后更新的时间戳。 不过我不太理解获取时间戳的过程。
I have been given the task of converting a C++ script into python script. The aim of the script is to loop through all the directories (by start and end date) in the mediaDB and calculate what size the zipfile is going to be. I am stuck at getting the for loop to go through the directories, its so different in python from C++ which i have more experience in. Could anyone offer any suggestions?
C++ Code
// This will loop over each core files directory and sum the file size.
directory_iterator dirIt(mediaDBCoreFilesDir);
for (directory_iterator dirIt(mediaDBCoreFilesDir);dirIt!=directory_iterator();dirIt++)
Also if anyone has any ideas as to how to get the last update timestamp from a file in python that would be very much appreciated. The C++ code is:
// Get the last update timestamp from the file
std::time_t t = last_write_time(*dirIt);
ptime fileTimeStamp = from_time_t(t);
EDIT: i am trying to write a for statement firstly that loops over all directories and sums up the file size of them. I dont need to edit, delete or print any directories, just get the file sizes. Is it then more appropriate to use os.walk and os.path.getsize?
Secondly, i need to retrieve the last updated timestamp from the files.
Tho i dont really understand this process of getting the timestamp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找
os.path.walk
或
glob.glob
用于枚举目录子树中的文件,以及os.stat
(或os. lstat
)用于获取最近修改的时间戳。You're looking for
os.path.walk
orglob.glob
for enumerating files in a directory subtree, andos.stat
(oros.lstat
) for getting the timestamp of the most recent modification.