【问题标题】:Can't run some python file out of directory无法在目录外运行一些 python 文件
【发布时间】:2023-02-17 02:54:48
【问题描述】:

你好,我尝试在目录外运行这个克隆的应用程序,但它不起作用:

https://github.com/laramies/theHarvester

我在 /opt/ 文件夹中克隆了这个应用程序并将 chmod -x 添加到主题我尝试通过这个命令运行它们:

python3 /opt/theHarvester/theHarvester.py

它给我这个错误:

  File "/opt/theHarvester/theHarvester.py", line 5, in <module>
    from theHarvester import __main__
  File "/opt/theHarvester/theHarvester/__main__.py", line 4, in <module>
    from theHarvester.discovery import dnssearch, takeover, shodansearch
  File "/opt/theHarvester/theHarvester/discovery/takeover.py", line 1, in <module> 
    from theHarvester.lib.core import *
 File "/opt/theHarvester/theHarvester/lib/core.py", line 238, in <module>
    class AsyncFetcher:
 File "/opt/theHarvester/theHarvester/lib/core.py", line 239, in AsyncFetcher
    proxy_list = Core.proxy_list()
  File "/opt/theHarvester/theHarvester/lib/core.py", line 103, in proxy_list
    with open('proxies.yaml', 'r') as proxy_file:
FileNotFoundError: [Errno 2] No such file or directory: 'proxies.yaml'`

但是当我使用这个命令时:

cd /opt/theHarvester/ python3 theHarvester.py

它运作良好。

【问题讨论】:

  • cd /opt/theHarvester/ 基本上将您的工作目录更改为 theHarvester.py 文件的位置,并且它能够在其中检测到 proxies.yaml。当您使用 python3 /opt/theHarvester/theHarvester.py 运行它时,您的 working_directory 不同并且 proxies.yaml 可能不存在于其中。
  • 谢谢,但是我该如何解决呢?我需要在一行中运行这个脚本。
  • cd /opt/theHarvester/; python3 theHarvester.py
  • 在你的 python 脚本中,而不是将文件引用为 proxies.yaml,你可以定义它的绝对路径,使用脚本本身的绝对路径使用 __file__。基本上,file = os.path.join(os.path.dirname(__file__), 'proxies.yaml')

标签: python linux bash ubuntu


【解决方案1】:

在 python 脚本的开头添加以下行:

script_directory = os.path.dirname(__file__)
os.chdir(script_directory)

这些行假装您从包含脚本的目录(也包括proxies.yaml)运行脚本。

【讨论】:

    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-17
    • 1970-01-01
    • 2012-12-15
    • 2021-03-08
    相关资源
    最近更新 更多