简体   繁体   English

为什么我的 python 程序运行在 Powershell 而不是 Bash? 使用 WSL

[英]Why does my python program run with Powershell but not Bash? Using WSL

I have a simple 3 line python program that I am trying to run.我有一个简单的 3 行 python 程序,我正在尝试运行。 It will run in Powershell but not in Bash. All it does is opens a text file and prints the info out in the terminal.它将在 Powershell 中运行,但不会在 Bash 中运行。它所做的只是打开一个文本文件并在终端中打印信息。

I am using WSL.我正在使用 WSL。

with open('C:/Users/me/Desktop/data.txt') as a:
    content = a.read()
    print(content)

I write "python C:/Users/me/Desktop/program.py" and it runs in the shell when I am using Powershell.我写“python C:/Users/me/Desktop/program.py”,当我使用 Powershell 时,它在 shell 中运行。

However once I switch the shell to Bash and run "python3 directory/program.py" it says "File "C:/Users/me/Desktop/program.py", line 1, in with open('C:/Users/me/Desktop/data.txt') as a: FileNotFoundError [Errno 2] No such file or directory: 'C:/Users/me/Desktop/data.txt'.但是,一旦我将 shell 切换为 Bash 并运行“python3 directory/program.py”,它会显示“文件”C:/Users/me/Desktop/program.py”,第 1 行,打开('C:/Users/ me/Desktop/data.txt') 作为:FileNotFoundError [Errno 2] 没有这样的文件或目录:'C:/Users/me/Desktop/data.txt'。

As a note, for some reason I need to type python3 rather than python when using Bash for it to even run my program, but in Powershell python rather than python3 works.请注意,出于某种原因,在使用 Bash 时我需要键入 python3 而不是 python 才能运行我的程序,但在 Powershell 中 python 而不是 python3 有效。

So I am just wondering why in Bash the program is found and runs, but the text file itself it says it cannot find.所以我只是想知道为什么在 Bash 中找到并运行了程序,但它说找不到文本文件本身。 But Powershell does find and run my program including finding the text file it reads.但是 Powershell 确实找到并运行了我的程序,包括找到它读取的文本文件。

Thank you谢谢

C:/Users/me/Desktop/data.txt isn't a valid path in the WSL filesystem afaik. C:/Users/me/Desktop/data.txt在 WSL 文件系统 afaik 中不是有效路径。 Try /mnt/c/Users/me/Desktop/data.txt - or make it work on both platforms by using os.path.expanduser :试试/mnt/c/Users/me/Desktop/data.txt - 或者使用os.path.expanduser让它在两个平台上工作:

import os
filename=os.path.expanduser('~') + '/Desktop/data.txt'

with open(filename) as a:
    content = a.read()
    print(content)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 多处理:为什么使用 Process 从一开始就运行我的程序? - Python multiprocessing: Why does using Process run my program from the start? 如何让`python`在WSL bash中运行Python 3? - How to get `python` to run Python 3 in WSL bash? 为什么我的 WSL Ubuntu 指向不同的 Python 版本? - Why does my WSL Ubuntu point to a different Python Version? python - 如何运行我的程序结束时不退出的bash文件python - How to run a bash file that does not exited when my program ends python 为什么我在运行PowerShell程序时,PowerShell在后台运行? - Why does PowerShell run in the background when I'm running a Python program? 为什么我的程序没有第二次运行for循环? - Why my program does not run second for loop? 为什么我的程序退出而不是永远运行 - why does my program quit and not run forever 当我使用Process.Start运行python程序时,我的python程序中的日志不起作用吗? - When I run a python program using Process.Start the log in my python program does not work? 从 python 中使用 WSL bash - Using WSL bash from within python 为什么Python的unittest在WSL bash中给出了“ImportError:不受文件名导入。” - Why does Python's unittest give “ImportError: Import by filename is not supported.” in WSL bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM