获取包含修饰功能的文件的路径

时间:2018-12-07 02:17:55

标签: python path python-decorators

我正在编写Python装饰器,我想在运行时知道包含装饰器的所有文件的名称。装饰器可能与使用装饰器的文件位于同一模块中。

更具体地说,我实际上想要这些文件的路径,因为我想确定是否对这些文件进行了未提交的git更改(假设它们正在使用git)。然后我可以使用

git -C <path> statusGitPython vel Sim。

我知道os.path.abspath,但是使用它需要以某种方式传递文件名,而通过装饰器似乎不可行。

1 个答案:

答案 0 :(得分:1)

您可以使用pycollect

from pycollect import PythonFileCollector

collector = PythonFileCollector()
files = []
for entry in collector.collect("../some/base/search/path"):
    with open(entry) as file:
        if "@my_decorator" in file.read():
            files.append(entry)
do_stuff(files)

它与操作系统无关,PythonFileCollector::collect会向您返回os.DirEntry的列表,您可以将其用作类路径对象。

pycollect is available at PyPI

pip install pycollect

公开:我正在github.com/allrod5/pycollect积极维护此库。

相关问题