【问题标题】:Python can't find file in Windows Subsystem for LinuxPython 在 Linux 的 Windows 子系统中找不到文件
【发布时间】:2020-03-19 08:45:01
【问题描述】:

我在 WSL 的 Ubuntu 环境中使用 python 编写了一个脚本。

我在 Windows 中下载了 firebase 的私钥“serviceAccountKey.json”,然后将其移动到我在 wsl 中的工作目录中。 (使用'mv mnt/c/Users/Yiu/Downloads/serviceAccountKey.json ~/projects/scrape')。现在,我正在尝试将该文件传递给 Certificate() 但我得到一个文件未找到错误。

我做了一些研究,发现权限可能存在一些问题,但文件都有读取、写入和执行权限。

脚本:

import requests
from bs4 import BeautifulSoup
import firebase_admin
from firebase_admin import credentials, firestore

cred = credentials.Certificate("~/projects/scrape/serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()

如何摆脱文件未找到错误?

【问题讨论】:

  • 你的路径正确吗?
  • 你检查你运行的是哪个python吗?在 WSL 中你可能偶尔会运行 Windows python,它只是不理解带有 ~ 的路径。
  • 是的,我的路径是正确的
  • 如何检查我正在运行的 python?
  • > which python

标签: python firebase google-cloud-firestore windows-subsystem-for-linux


【解决方案1】:

您需要设置绝对路径,因为~会因调用用户而异

cred = credentials.Certificate("~/projects/scrape/serviceAccountKey.json")

【讨论】:

  • 如何使用绝对路径?在我的情况下,它是 \\wsl$\debian\home\u 在 python 中不起作用,找不到带有 \. 的路径
【解决方案2】:

~ 通常由 shell 扩展为 $HOME 的值。这不适用于您的程序中的路径,包括 Python 程序,除非它们也进行类似的替换; Python 在pathlib.Path.expanduseros.path.expanduser 中为此提供了实用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 2017-10-01
    • 2020-11-19
    • 1970-01-01
    • 2019-05-17
    • 2018-04-19
    • 1970-01-01
    相关资源
    最近更新 更多