使用shell脚本或者python检查目录内容是否改变

发布于 2024-10-15 01:21:44 字数 127 浏览 1 评论 0原文

我有一个在特定目录中创建文件的程序。 当这些文件准备好后,我运行 Latex 来生成一个 .pdf 文件。 所以,我的问题是,如何使用此目录更改作为触发器 使用 shell 脚本或 python 脚本调用 Latex?

此致

I have a program that create files in a specific directory.
When those files are ready, I run Latex to produce a .pdf file.
So, my question is, how can I use this directory change as a trigger
to call Latex, using a shell script or a python script?

Best Regards

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心是晴朗的。 2024-10-22 01:21:45

我自己并不是一个蟒蛇人。但在紧要关头,假设您使用的是 Linux,您可以定期执行“ls -lrt /path/to/directory”(获取目录内容并按上次修改排序),并比较最后两次调用的结果为了区别。如果是这样,那就有变化了。不是很详细,但完成了工作。

Not much of a python man myself. But in a pinch, assuming you're on linux, you could periodically shell out and "ls -lrt /path/to/directory" (get the directory contents and sort by last modified), and compare the results of the last two calls for a difference. If so, then there was a change. Not very detailed, but gets the job done.

旧夏天 2024-10-22 01:21:45

您可以使用本机Python模块hashlib来实现MD5算法:

>>> import hashlib
>>> import os
>>> m = hashlib.md5()
>>> for root, dirs, files in os.walk(path):
    for file_read in files:
        full_path = os.path.join(root, file_read)
        for line in open(full_path).readlines():
            m.update(line)


>>> m.digest()
'pQ\x1b\xb9oC\x9bl\xea\xbf\x1d\xda\x16\xfe8\xcf'

您可以将此结果保存在文件或变量中,并将其与下次运行的结果进行比较。这将检测任何子目录中任何文件的更改。

这没有考虑文件权限的更改;如果您还需要监视这些更改,可以通过将表示权限的字符串(例如,可通过 os.stat 访问,属性取决于您的系统)附加到 m< /代码>变量。

You can use native python module hashlib which implements MD5 algorithm:

>>> import hashlib
>>> import os
>>> m = hashlib.md5()
>>> for root, dirs, files in os.walk(path):
    for file_read in files:
        full_path = os.path.join(root, file_read)
        for line in open(full_path).readlines():
            m.update(line)


>>> m.digest()
'pQ\x1b\xb9oC\x9bl\xea\xbf\x1d\xda\x16\xfe8\xcf'

You can save this result in a file or a variable, and compare it to the result of the next run. This will detect changes in any files, in any sub-directory.

This does not take into account file permission changes; if you need to monitor these change as well, this could be addressed via appending a string representing the permissions (accessible via os.stat for instance, attributes depend on your system) to the mvariable.

GRAY°灰色天空 2024-10-22 01:21:44

inotify 取代 dnotify

为什么?

...dnotify 需要为您想要监视更改的每个目录打开一个文件描述符...

此外,文件描述符固定目录,不允许卸载支持设备,这会在涉及可移动媒体的情况下导致问题。使用 inotify 时,如果您正在监视已卸载的文件系统上的文件,则监视会自动删除,并且您会收到卸载事件。

...以及更多。

更多原因?

与它的祖先 dnotify 不同,inotify 不会因各种限制而使您的工作复杂化。例如,如果您观看可移动媒体上的文件,这些文件不会被锁定。与此相比,dnotify 要求文件本身打开,因此真正“锁定”它们(阻碍卸载媒体)。

参考

inotify replaces dnotify.

Why?

...dnotify requires opening one file descriptor for each directory that you intend to watch for changes...

Additionally, the file descriptor pins the directory, disallowing the backing device to be unmounted, which causes problems in scenarios involving removable media. When using inotify, if you are watching a file on a file system that is unmounted, the watch is automatically removed and you receive an unmount event.

...and more.

More Why?

Unlike its ancestor dnotify, inotify doesn't complicate your work by various limitations. For example, if you watch files on a removable media these file aren't locked. In comparison with it, dnotify requires the files themselves to be open and thus really "locks" them (hampers unmounting the media).

Reference

谜泪 2024-10-22 01:21:44

dnotify 是您所需要的吗?

Is dnotify what you need?

凤舞天涯 2024-10-22 01:21:44

UNIX 系统上的 Make 通常用于按日期跟踪文件更改时需要重建的内容。我通常使用相当好的makefile来完成这项工作。谷歌代码上似乎还有另一个替代方案

Make on unix systems is usually used to track by date what needs rebuilding when files have changed. I normally use a rather good makefile for this job. There seems to be another alternative around on google code too

夢归不見 2024-10-22 01:21:44

您不仅需要检查更改,还需要在运行 LaTeX 之前知道所有更改均已完成。例如,如果您在第一个文件修改后启动 LaTeX,而更多更改仍待处理,则您将使用部分数据,并且必须稍后重新运行。

等待第一个程序完成:

#!/bin/bash
first-program &&
  run-after-changes-complete

使用 &&表示仅当第一个命令成功完成(零退出代码)时才执行第二个命令。因为这个简单的脚本将始终运行第二个命令,即使第一个命令没有更改任何文件,因此您可以将其合并到您已经熟悉的任何构建系统中,例如 make。

You not only need to check for changes, but need to know that all changes are complete before running LaTeX. For example, if you start LaTeX after the first file has been modified and while more changes are still pending, you'll be using partial data and have to re-run later.

Wait for your first program to complete:

#!/bin/bash
first-program &&
  run-after-changes-complete

Using && means the second command is only executed if the first completes successfully (a zero exit code). Because this simple script will always run the second command even if the first doesn't change any files, you can incorporate this into whatever build system you are already familiar with, such as make.

書生途 2024-10-22 01:21:44

Python FAMFAM(文件更改监视器)

您还可以查看 Pyinotify,这是一个用于监控文件系统变化的模块。

Python FAM is a Python interface for FAM (File Alteration Monitor)

You can also have a look at Pyinotify, which is a module for monitoring file system changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文