-3

When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.

Thanks very much!

It seems that I have tried everything, and nothing is working. :(

A. Mist
  • 23
  • 1
  • 5

3 Answers3

1

In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space

Kidding!

You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py
Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command.

Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory.

Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.

akash12300
  • 412
  • 2
  • 12
1

I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.

Opening your terminal's respective shell, enter the following on your command line:

  1. cd Desktop [to change directory to your desktop]
  2. ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
  3. python hello.py [to run your python file]

That should run it. Let me know if you run into errors.

Community
  • 1
  • 1
jimmytt
  • 236
  • 2
  • 13
0

I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:

First open your terminal, then type cd ~/Desktop. Now try running your python script.

EDIT:

Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)

Steampunkery
  • 3,839
  • 2
  • 19
  • 28