简体   繁体   English

无法打开文件'file.py':[Errno 2] 没有这样的文件或目录

[英]Can't open file 'file.py': [Errno 2] No such file or directory

I'm using Windows10 and wants to run my python script by cmd我正在使用 Windows10 并想通过 cmd 运行我的 python 脚本

The command:命令:

python file.py

return the message:返回消息:

python: can't open file 'file.py': [Errno 2] No such file or directory

but when I run python C:\Python37\projects\file.py it's run properly但是当我运行python C:\Python37\projects\file.py它运行正常

also, I added C:\Python37\projects to the PATH, but it doesn't help另外,我将C:\Python37\projects添加到 PATH 中,但没有帮助

When you run a script from the prompt you need to use the whole path to the file (as you did when running python C:\Python37\projects\file.py) or you need to first move to the directory containing the file and then run, otherwise you rise the error you got (cause there's no file with such name in your current directory).当您从提示符运行脚本时,您需要使用文件的整个路径(就像您在运行 python C:\Python37\projects\file.py 时所做的那样),或者您需要首先移动到包含文件的目录,然后运行,否则会出现错误(因为当前目录中没有具有此类名称的文件)。 You can move to another directory using the command cd.您可以使用命令 cd 移动到另一个目录。 So what you could do if you don't want to type the path every time you run the code is:因此,如果您不想在每次运行代码时都键入路径,您可以做的是:

cd C:\Python37\projects
python file.py

You can set an environment variable PYTHONPATH to a directory where you put your scripts.您可以将环境变量 PYTHONPATH 设置为放置脚本的目录。 Then you can run your script just as然后你可以运行你的脚本就像

python script.py

Open your script in another code editor like VS and check extension.在另一个代码编辑器(如 VS)中打开您的脚本并检查扩展名。 It usually keeps txt behind py and that is why you get an error.它通常将 txt 保留在 py 后面,这就是您收到错误的原因。 Or in Options, if you use Windows, Hide extensions for known file types.或者在选项中,如果您使用 Windows,隐藏已知文件类型的扩展名。

Check out the environment you have done your installation.检查您已完成安装的环境。 Then call the path of your directory rightly.然后正确调用你的目录的路径。

cd C:\Python37\projects cd C:\Python37\projects

python file.py蟒蛇文件.py

暂无
暂无

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

相关问题 如何解决 docker 的 PyCharm 错误“无法打开文件 'path/to/file.py': [Errno 2] No such file or directory”? - How to solve PyCharm error with docker "can't open file 'path/to/file.py': [Errno 2] No such file or directory"? 无法打开文件“import.py”:[Errno 2] 没有这样的文件或目录 - can't open file 'import.py' :[Errno 2] No such file or directory 无法打开文件 'main.py': [Errno 2] 没有那个文件或目录 - Can't open file 'main.py': [Errno 2] No such file or directory 无法打开文件 'manage.py': [Errno 2] 没有那个文件或目录 - Can't open file 'manage.py': [Errno 2] No such file or directory 无法打开文件'menu.py':[Errno 2]没有这样的文件或目录 - Can't open file 'menu.py': [Errno 2] No such file or directory 无法打开文件'.manage.py':[Errno 2] 没有这样的文件或目录 - can't open file '.manage.py': [Errno 2] No such file or directory 无法打开文件“audioAnalysis.py”:[Errno 2] 没有这样的文件或目录 - Can't open file 'audioAnalysis.py': [Errno 2] No such file or directory 无法打开文件 'app.py': [Errno 2] 没有那个文件或目录 - can't open file 'app.py': [Errno 2] No such file or directory Python-exec(open('file.py')。read())报告错误-没有此类文件或目录。 'file.py' - Python - exec(open('file.py').read()) reports error - No such file or directory. 'file.py' 无法打开文件:[Errno 2] 没有那个文件或目录 - can't open file : [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM