【问题标题】:Where should I store the path of a file, that my python program found?我应该在哪里存储我的 python 程序找到的文件的路径?
【发布时间】:2021-09-09 11:53:03
【问题描述】:

假设,如果我想使用 python 打开 chrome。我需要“chrome.exe”的路径。我的 python 程序通过搜索程序文件中的 chrome 目录来做到这一点。

如果我不想在目录中搜索,则每次运行程序时。我假设我必须将“chrome.exe”的路径保存在某处。

我应该在哪里这样做?我应该将它存储在与我的程序相同的文件夹中的 .txt 文件中,还是将其存储在其他地方?我不希望任何人更改路径,因此它会导致我的程序出错。我如何将它永久存储在某个地方,除了我的程序之外没有人可以编辑它?

【问题讨论】:

  • 在您的程序中作为字符串变量。或者,如果您不想公开您的目录结构,则作为 .env 中的变量。

标签: python file google-chrome directory path


【解决方案1】:

为什么不在这个 python 程序中保存路径,然后你可以用 Chrome 的路径调用 get()。

将 chrome_path 替换为您平台的正确路径。

import webbrowser

url = 'http://docs.python.org/'

# Windows
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# MacOS
#chrome_path = 'open -a /Applications/Google\ Chrome.app %s'


# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-21
    • 2011-02-15
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 2017-05-30
    • 2012-03-09
    • 2011-06-27
    相关资源
    最近更新 更多