Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何检查更新的文件并显示其内容?_Python - Fatal编程技术网

Python 如何检查更新的文件并显示其内容?

Python 如何检查更新的文件并显示其内容?,python,Python,我有一个文本文件,想检查它是否更新,并显示更新的内容 我尝试使用上次修改的时间并获取校验和,但如何存储它并在下次使用它 last_modified_date = datetime.fromtimestamp(mtime) hash1 = md5.new() hash1.update("textfile.txt") prevchecksum = hash1.digest() 如果您使用的是unix,可能stat和diff命令可以执行以下操作: root@Mc:~# echo "new" >

我有一个文本文件,想检查它是否更新,并显示更新的内容

我尝试使用上次修改的时间并获取校验和,但如何存储它并在下次使用它

last_modified_date = datetime.fromtimestamp(mtime)
hash1 = md5.new()
hash1.update("textfile.txt")
prevchecksum = hash1.digest()

如果您使用的是
unix
,可能
stat
diff
命令可以执行以下操作:

root@Mc:~# echo "new" > a
root@Mc:~# echo "old" > b
root@Mc:~# diff a b
1c1
< new
---
> old
root@Mc:~# stat a
  File: 'a'
  Size: 4           Blocks: 8          IO Block: 4096   regular file
Device: fc01h/64513d    Inode: 1346        Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-05-05 06:52:52.898107822 +0000
Modify: 2017-05-05 06:52:48.434076120 +0000
Change: 2017-05-05 06:52:48.434076120 +0000
 Birth: -

希望这有帮助。

什么是“下次”?下次运行脚本时?此脚本将在每小时运行但如何比较上次和上次修改的时间?如何获取旧的时间content@tintin也许您可以使用循环来存储最后一个哈希值或文件内容以及每10秒修改一次的时间。
>>> import subprocess
>>> status = subprocess.check_output(['stat','a'])
>>> status
"  File: 'a'\n  Size: 4         \tBlocks: 8          IO Block: 4096   regular file\nDevice: fc01h/64513d\tInode: 1346        Links: 1\nAccess: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)\nAccess: 2017-05-05 06:52:52.898107822 +0000\nModify: 2017-05-05 06:52:48.434076120 +0000\nChange: 2017-05-05 06:52:48.434076120 +0000\n Birth: -\n"