获取Python的最后修改日期

时间:2022-09-25 14:03:48

I'm trying to get the last modification date of a usb drive in Python. I am aware of os.path.getmtime() and os.stat(), but those only work on folders and files. When I run os.path.getmtime() on the usb drive's root path I get either "Mon Dec 31 23:00:00 1979" (on Windows 7) or a date in 1970 (on Linux).

我试图在Python中获取usb驱动器的最后修改日期。我知道os.path.getmtime()和os.stat(),但这些只适用于文件夹和文件。当我在usb驱动器的根路径上运行os.path.getmtime()时,我得到“Mon Dec 31 23:00:00 1979”(在Windows 7上)或1970年的某个日期(在Linux上)。

Looping through the content and finding the most recently modified file won't help since I can modify the content of the drive by adding and removing files without actually changing their last modification date.

循环浏览内容并找到最近修改过的文件将无济于事,因为我可以通过添加和删除文件来修改驱动器的内容,而无需实际更改其上次修改日期。

I need this because I am writing a script that requires the files and folders on the usb drive to be indexed in a database. This can take some time if there are many items, so I only want to re-index the content if it has changed since the drive was last used by the script.

我需要这个,因为我正在编写一个脚本,要求usb驱动器上的文件和文件夹在数据库中编入索引。如果有很多项目,这可能需要一些时间,所以我只想重新索引内容,如果它自上次使用该驱动器以来已经发生了变化。

Edit : the script plays audio files (from the USB drive or elsewhere), which aren't very likely to change. What really matters is to check whether the content of the drive has changed (if audio files and folders were added or deleted). The content gets indexed by the script because it needs a unique id for each file (on the USB drive or not) to make playlists.

编辑:脚本播放音频文件(来自USB驱动器或其他地方),这些文件不太可能发生变化。真正重要的是检查驱动器的内容是否已更改(如果添加或删除了音频文件和文件夹)。内容由脚本编制索引,因为它需要每个文件(在USB驱动器上或不在USB驱动器上)的唯一ID来制作播放列表。

Thanks in advance for your help.

在此先感谢您的帮助。

2 个解决方案

#1


you could use the hashlib and glob libraries to loop thru the files and caclulate out a hash for each one and store it in a file somewhere. then check the currently calced hash vs the previous one, if they are different perform the database update

你可以使用hashlib和glob库来循环遍历文件并为每个文件计算一个哈希并将其存储在某个文件中。然后检查当前被调用的哈希与前一个哈希,如果它们不同则执行数据库更新

#2


Instead of finding the last modified file, just find the last "event" on the file using max(os.lstat(file)[-3:]) :

而不是找到最后修改的文件,只需使用max(os.lstat(file)[ - 3:])找到文件上的最后一个“事件”:

import datetime
import os

volume = 'F:/'

#simply yelds all files in all subdirs of the volume
def all_files(volume):
    for root, dirnames, filenames in os.walk(volume):
        for filename in filenames:
            yield os.path.join(root, filename)

print datetime.datetime.fromtimestamp(max((max(os.lstat(file)[-3:]) for file in all_files(volume))))

works in my system (WinXP), I tried to modify create o copy/paste files in the volume.

在我的系统(WinXP)中工作,我试图在卷中修改创建o复制/粘贴文件。

Does not work with file ereasing.

不适用于文件备份。

#1


you could use the hashlib and glob libraries to loop thru the files and caclulate out a hash for each one and store it in a file somewhere. then check the currently calced hash vs the previous one, if they are different perform the database update

你可以使用hashlib和glob库来循环遍历文件并为每个文件计算一个哈希并将其存储在某个文件中。然后检查当前被调用的哈希与前一个哈希,如果它们不同则执行数据库更新

#2


Instead of finding the last modified file, just find the last "event" on the file using max(os.lstat(file)[-3:]) :

而不是找到最后修改的文件,只需使用max(os.lstat(file)[ - 3:])找到文件上的最后一个“事件”:

import datetime
import os

volume = 'F:/'

#simply yelds all files in all subdirs of the volume
def all_files(volume):
    for root, dirnames, filenames in os.walk(volume):
        for filename in filenames:
            yield os.path.join(root, filename)

print datetime.datetime.fromtimestamp(max((max(os.lstat(file)[-3:]) for file in all_files(volume))))

works in my system (WinXP), I tried to modify create o copy/paste files in the volume.

在我的系统(WinXP)中工作,我试图在卷中修改创建o复制/粘贴文件。

Does not work with file ereasing.

不适用于文件备份。