Python Forum
No such file or directory
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
No such file or directory
#1
Greetings,

I just installed Python 3.7 (and marked the optional "add Python to PATH" during the installation).
I can open the Python interpreter in the terminal, but it doesn't work with scripts.

I wrote a file named "hello.py" with the command "print("hello world")" and saved it in a folder named "testpy" on C.

In cmd I enter the command "python C:\testpy\hello.py" but I always get the error message:
[Errno2] No such file directory.


I did not make any manual changes to system variables or such, I thought the mark during the installation was enough??

Regards
SL
Reply
#2
try to change your working directory to be C:\testpy\
cd C:\testpy

then run just python hello.py

or don't use backslash, but forward slash in the filepath
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Jul-26-2018, 01:52 PM)buran Wrote: try to change your working directory to be C:\testpy\
cd C:\testpy

then run just python hello.py

or don't use backslash, but forward slash in the filepath

Thanks for the response, but does not work either =(
Using Win 7, btw.
Reply
#4
Here some test you can do in cmd(do not like to use cmd when cmder is bye far the best shell in Windows).
# cd in
C:\>cd testpy

# Look at files
C:\testpy>dir
 Volume in drive C has no label.
 Volume Serial Number is EED7-45CC

 Directory of C:\testpy

26.07.2018  21:04    <DIR>          .
26.07.2018  21:04    <DIR>          ..
26.07.2018  21:04                20 hello.py
               1 File(s)             20 bytes
               2 Dir(s)  54 170 333 184 bytes free

# What the file contain
C:\testpy>more hello.py
print('hello world')

# What python is set in Path
C:\testpy>python -c "import sys; print(sys.executable)"
C:\python37\python.exe

# Test also pip
C:\testpy>pip -V
pip 10.0.1 from c:\python37\lib\site-packages\pip (python 3.7)

# Run when in folder of file
C:\testpy>python hello.py
hello world

# cd out
C:\testpy>cd ..

# Run outside of folder,bye give path to file
C:\>python c:\testpy\hello.py
hello world
A couple of installation tutorials part1, part2.
Reply
#5
Sorry I don't understand. Should I just copy'n paste this code into cmd or what?

edit: ah, ok, I try it one by one.

If I try:
# What the file contain
C:\testpy>more hello.py


It says that the file cannot be accessed?
Reply
#6
(Jul-31-2018, 12:18 PM)SchroedingersLion Wrote: It says that the file cannot be accessed?
Do you have a file called hello.py in folder testpy?
If i fire up cmd:
Microsoft Windows [Version 10.0.17134.165]
(c) 2018 Microsoft Corporation. Med enerett.

C:\Windows\System32>cd\

# Cd in to testpy
C:\>cd testpy

# Look at file in folder
C:\testpy>dir
 Volume in drive C has no label.
 Volume Serial Number is EED7-45CC

 Directory of C:\testpy

26.07.2018  21:04    <DIR>          .
26.07.2018  21:04    <DIR>          ..
26.07.2018  21:04                20 hello.py
               1 File(s)             20 bytes
               2 Dir(s)  54 074 580 992 bytes free

# Look at file
C:\testpy>more hello.py
print('hello world')

# Run the file
C:\testpy>more hello.py
print('hello world')

# If try with a file that's not there,then get same message as you
C:\testpy>more bar.py
Cannot access file C:\testpy\bar.py
Quote:edit: ah, ok, I try it one by one.
This is basic command line stuff that most be learned as is a important part of learning programming.
Windows only user to struggle with this,it's a more natural part when using Linux.
cmd/powershell that comes with Windows is not pleasant to use at all,
that's why i use a better tool(cmder) that works as good as any Terminal shell on Linux.
Reply
#7
(Jul-31-2018, 05:59 PM)snippsat Wrote:
(Jul-31-2018, 12:18 PM)SchroedingersLion Wrote: It says that the file cannot be accessed?
Do you have a file called hello.py in folder testpy?
Yes, I definitely do.
hello.py in C:\testpy

(Jul-31-2018, 05:59 PM)snippsat Wrote: If i fire up cmd:
Microsoft Windows [Version 10.0.17134.165]
(c) 2018 Microsoft Corporation. Med enerett.

C:\Windows\System32>cd\

# Cd in to testpy
C:\>cd testpy

# Look at file in folder
C:\testpy>dir
 Volume in drive C has no label.
 Volume Serial Number is EED7-45CC

 Directory of C:\testpy

26.07.2018  21:04    <DIR>          .
26.07.2018  21:04    <DIR>          ..
26.07.2018  21:04                20 hello.py
               1 File(s)             20 bytes
               2 Dir(s)  54 074 580 992 bytes free

# Look at file
C:\testpy>more hello.py
print('hello world')

# Run the file
C:\testpy>more hello.py
print('hello world')

# If try with a file that's not there,then get same message as you
C:\testpy>more bar.py
Cannot access file C:\testpy\bar.py
Quote:edit: ah, ok, I try it one by one.
This is basic command line stuff that most be learned as is a important part of learning programming.
Windows only user to struggle with this,it's a more natural part when using Linux.
cmd/powershell that comes with Windows is not pleasant to use at all,
that's why i use a better tool(cmder) that works as good as any Terminal shell on Linux.
I know, usually I use an IDE and, sadly, I am still using Windows ~~
I will switch to Linux as soon as I have enough time.
Reply
#8
SchroedingersLion Wrote:Yes, I definitely do.
hello.py in C:\testpy
Run cmd as administrator
Follow all commands i do,before more check with dir that the file is there.
You should get this to work.
Reply
#9
(Aug-01-2018, 04:52 PM)snippsat Wrote:
SchroedingersLion Wrote:Yes, I definitely do.
hello.py in C:\testpy
Run cmd as administrator
Follow all commands i do,before more check with dir that the file is there.
You should get this to work.

I tried, but still the same error. Using 'dir', I see the same as you, that the file is definitely there...
Reply
#10
Hmm i can not understand what happens for you,what happens if you try to run the file?
Here a couple way with give path and run when in folder folder.
Start cmd:
# Give path and run hello.py 
C:\WINDOWS\system32>python C:\testpy\hello.py
hello world

# cd into testpy 
C:\WINDOWS\system32>cd C:\testpy

# Run when in folder of hello.py
C:\testpy>python hello.py
hello world

# cd to root C:
C:\testpy>cd\

# Test where python run from,these command should be same no matter what folder you into in cmd
C:\>python -c "import sys; print(sys.executable)"
C:\python37\python.exe

# Test pip
C:\>pip -V
pip 18.0 from c:\python37\lib\site-packages\pip (python 3.7)

# cd into testpy
C:\>cd testpy

# List files
C:\testpy>dir
 Volume in drive C has no label.
 Volume Serial Number is EED7-45CC

 Directory of C:\testpy

26.07.2018  21:04    <DIR>          .
26.07.2018  21:04    <DIR>          ..
26.07.2018  21:04                20 hello.py
               1 File(s)            20 bytes 
              2 Dir(s)  53 071 597 568 bytes free


# Spelling error
C:\testpy>more hell.py
Cannot access file C:\testpy\hell.py

# Ok
C:\testpy>more hello.py
print('hello world')
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020