如何修复FileNotFoundError[WinError 2]?

2024-04-24 16:33:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将Proton VPN Linux CLI(https://github.com/ProtonVPN/linux-cli)移植到Windows,到目前为止已经取得了不错的成功,但是我遇到了[subprocess.Popen()]的问题。我尝试了许多修复方法,但迄今为止没有一个有效。我已尝试将其转换为:

    with open(os.path.join(CONFIG_DIR, "ovpn.log"), "w+") as f:
        subprocess.Popen(
            [
                "openvpn",
                "--config", OVPN_FILE,
                "--auth-user-pass", PASSFILE,
                "--dev", "proton0",
                "--dev-type", "tun"
            ],
            stdout=f, stderr=f
        )

从最初的模块到以下模块:

    file_name = os.path.join(CONFIG_DIR, "ovpn.log")
    print(file_name)
    with open(file_name, "w+") as f:
        subprocess.Popen([
            "openvpn",
            "--config", OVPN_FILE,
            "--auth-user-pass", PASSFILE,
            "--dev", "proton0",
            "--dev-type", "tun"
        ], stdout=f, stderr=f)

两者都会产生相同的错误:

Traceback (most recent call last):
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "C:\Users\<user>\AppData\Local\Programs\Python\Python39\Scripts\protonvpn.exe\__main__.py", line 7, in <module>
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\protonvpn_cli\cli.py", line 73, in main
    cli()
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\protonvpn_cli\cli.py", line 114, in cli
    connection.fastest(protocol)
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\protonvpn_cli\connection.py", line 163, in fastest
    openvpn_connect(fastest_server, protocol)
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\site-packages\protonvpn_cli\connection.py", line 463, in openvpn_connect
    subprocess.Popen([
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\subprocess.py", line 947, in __init__
    self._execute_child(args, executable, preexec_fn, close_fds,
  File "c:\users\<user>\appdata\local\programs\python\python39\lib\subprocess.py", line 1416, in _execute_child
    hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

有人能帮我吗?我已经为此挣扎了一整天,但一直找不到解决办法。(顺便说一下,我对python非常陌生)

编辑:在尝试从原始模块打印“f”时,它返回以下内容-“<;io.TextIOWrapper name='C:\Users\\.pvpn cli\ovpn.log'mode='w+'encoding='cp1252'>;”。我尝试将名称输入到浏览器选项卡中,收到一个错误,但在删除双斜杠后,我被带到文件中。我很确定双斜线应该在那里,但这可能是问题所在吗


Tags: inpycliliblocallineusersappdata