[英]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.