Python Forum
Cannot get cmd to print Python file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot get cmd to print Python file
#1
I have recently decided to learn python and found a playlist by Corey Schafer. The problem is that with the very first task, I cannot get it to work. He says to create a script in IDLE and then once saved you can print it by calling it from the cmd terminal.

C:\Users\brett>python Intro.py
python: can't open file 'C:\\Users\\brett\\Intro.py': [Errno 2] No such file or directory

C:\Users\brett>python C:\Users\brett\AppData\Local\Programs\Python\Python312\Intro
python: can't open file 'C:\\Users\\brett\\AppData\\Local\\Programs\\Python\\Python312\\Intro': [Errno 2] No such file or directory

C:\Users\brett>python Users\brett\AppData\Local\Programs\Python\Python312\Intro
python: can't open file 'C:\\Users\\brett\\Users\\brett\\AppData\\Local\\Programs\\Python\\Python312\\Intro': [Errno 2] No such file or directory

C:\Users\brett>clear
'clear' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\brett>python Desktop/Intro.py
python: can't open file 'C:\\Users\\brett\\Desktop\\Intro.py': [Errno 2] No such file or directory

C:\Users\brett>python Desktop/Python Files/Intro.py
python: can't open file 'C:\\Users\\brett\\Desktop\\Python': [Errno 2] No such file or directory

C:\Users\brett>python Desktop/PythonFiles/Intro.py
python: can't open file 'C:\\Users\\brett\\Desktop\\PythonFiles\\Intro.py': [Errno 2] No such file or directory

C:\Users\brett>python Desktop/PythonFiles/Intro
python: can't open file 'C:\\Users\\brett\\Desktop\\PythonFiles\\Intro': [Errno 2] No such file or directory

C:\Users\brett>python Documents/PythonFiles/Intro
python: can't open file 'C:\\Users\\brett\\Documents\\PythonFiles\\Intro': [Errno 2] No such file or directory

C:\Users\brett>python C:\Users\brett\OneDrive\Documents\PythonFiles
C:\Users\brett\AppData\Local\Programs\Python\Python312\python.exe: can't find '__main__' module in 'C:\\Users\\brett\\OneDrive\\Documents\\PythonFiles'

I have tried many different ways of loading it but the cmd never finds it. I tried it on my desktop then in my documents folder. I tried copying the directory address (I think it's called). It seems like a very stupid error, mainly because Corey Schafer seems to be quite thorough in his explanations but he did not explain this at all.
I'm sorry if I have posted this in the wrong forum or I wrote it wrong. This is my first time posting and I tried following the rules that I read. At the moment I don't know what BBCode is but I will also try to learn what that is.
Reply
#2
You'll find things easier if you change the working directory in you shell to the location of the python files.

Quote:C:\Users\brett>python C:\Users\brett\AppData\Local\Programs\Python\Python312\Intro
python: can't open file 'C:\\Users\\brett\\AppData\\Local\\Programs\\Python\\Python312\\Intro': [Errno 2] No such file or directory
Where are you saving your programs? You should not save your programs in a folder under your python directory. Make a folder that is separate from where you installed python, and save your program files there.
Reply
#3
(May-14-2024, 06:03 PM)deanhystad Wrote: You'll find things easier if you change the working directory in you shell to the location of the python files.

Quote:C:\Users\brett>python C:\Users\brett\AppData\Local\Programs\Python\Python312\Intro
python: can't open file 'C:\\Users\\brett\\AppData\\Local\\Programs\\Python\\Python312\\Intro': [Errno 2] No such file or directory
Where are you saving your programs? You should not save your programs in a folder under your python directory. Make a folder that is separate from where you installed python, and save your program files there.

I only saved it in the python directory the first time. Afterwards, I tried saving it on my desktop.

C:\Users\brett>python
Python 3.12.3 (tags/v3.12.3:f6650f9, Apr 9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Current Working Directory", os.getcwd())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined. Did you forget to import 'os'?
>>> import os
>>> print("Current Working Directory", os.getcwd())
Current Working Directory C:\Users\brett
>>> python C:\Users\brett\OneDrive\Desktop\Intro
File "<stdin>", line 1
python C:\Users\brett\OneDrive\Desktop\Intro
^
SyntaxError: invalid syntax
Here I tried changing the working directory but I always get an error.
I've tried attaching a picture of the directory path that keeps giving me errors

Attached Files

Thumbnail(s)
   
Reply
#4
(May-15-2024, 05:04 AM)Schauster Wrote: >>> python C:\Users\brett\OneDrive\Desktop\Intro
Don't call the 'python' command from within the Python interpreter (>>>). Call it directly from the command line (and don't forget the .py suffix)
Output:
C:\Users\brett>python C:\Users\brett\OneDrive\Desktop\Intro.py
« We can solve any problem by introducing an extra level of indirection »
Reply
#5
(May-15-2024, 06:02 AM)Gribouillis Wrote:
(May-15-2024, 05:04 AM)Schauster Wrote: >>> python C:\Users\brett\OneDrive\Desktop\Intro
Don't call the 'python' command from within the Python interpreter (>>>). Call it directly from the command line (and don't forget the .py suffix)
Output:
C:\Users\brett>python C:\Users\brett\OneDrive\Desktop\Intro.py

Ok so I tried to open the command prompt and tried both without typing python first, then it didn't have the three >>>. Afterwards, I typed python and got the 3 >>>, either way I got errors.

C:\Users\brett>C:\Users\brett\OneDrive\Desktop\Intro.py
'C:\Users\brett\OneDrive\Desktop\Intro.py' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\brett>python
Python 3.12.3 (tags/v3.12.3:f6650f9, Apr 9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> C:\Users\brett\OneDrive\Desktop\Intro.py
File "<stdin>", line 1
C:\Users\brett\OneDrive\Desktop\Intro.py
^
SyntaxError: unexpected character after line continuation character
Reply
#6
(May-15-2024, 08:17 AM)Schauster Wrote:
(May-15-2024, 06:02 AM)Gribouillis Wrote: Don't call the 'python' command from within the Python interpreter (>>>). Call it directly from the command line (and don't forget the .py suffix)
Output:
C:\Users\brett>python C:\Users\brett\OneDrive\Desktop\Intro.py

Ok so I tried to open the command prompt and tried both without typing python first, then it didn't have the three >>>. Afterwards, I typed python and got the 3 >>>, either way I got errors.

C:\Users\brett>C:\Users\brett\OneDrive\Desktop\Intro.py


'C:\Users\brett\OneDrive\Desktop\Intro.py' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\brett>python
Python 3.12.3 (tags/v3.12.3:f6650f9, Apr 9 2024, 14:05:25) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> C:\Users\brett\OneDrive\Desktop\Intro.py
File "<stdin>", line 1
C:\Users\brett\OneDrive\Desktop\Intro.py
^
SyntaxError: unexpected character after line continuation character

Here is another error doing it the way you suggested.

C:\Users\brett>python C:\Users\brett\OneDrive\Desktop\Intro.py
python: can't open file 'C:\\Users\\brett\\OneDrive\\Desktop\\Intro.py': [Errno 2] No such file or directory
Reply
#7
(May-15-2024, 08:31 AM)Schauster Wrote: can't open file 'C:\\Users\\brett\\OneDrive\\Desktop\\Intro.py': [Errno 2] No such file or directory
Is there no such file? I'm not a Windows user, but you need to enter a valid path to your program.
« We can solve any problem by introducing an extra level of indirection »
Reply
#8
(May-15-2024, 08:58 AM)Gribouillis Wrote:
(May-15-2024, 08:31 AM)Schauster Wrote: can't open file 'C:\\Users\\brett\\OneDrive\\Desktop\\Intro.py': [Errno 2] No such file or directory
Is there no such file? I'm not a Windows user, but you need to enter a valid path to your program.

I've attached a screenshot of the directory path and how I have it saved in a folder on my desktop.
This is frustrating, especially since it was in the very first python tutorial video I started watching on YouTube.

Attached Files

Thumbnail(s)
   
Reply
#9
(May-15-2024, 09:22 AM)Schauster Wrote: I've attached a screenshot of the directory path
Now it seems that it should be something like
Output:
python C:\Users\brett\OneDrive\Desktop\PythonFiles\Intro.py
« We can solve any problem by introducing an extra level of indirection »
Reply
#10
(May-15-2024, 09:30 AM)Gribouillis Wrote:
(May-15-2024, 09:22 AM)Schauster Wrote: I've attached a screenshot of the directory path
Now it seems that it should be something like
Output:
python C:\Users\brett\OneDrive\Desktop\PythonFiles\Intro.py

Thank you very much, it works now. So if I understood correctly, if I have the 3 >>>, then I cannot call python scripts? But if I am outside of the >>>, I can call them if I type python first then the correct path?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Saving the print result in a text file Calli 8 3,955 Sep-25-2022, 06:38 PM
Last Post: snippsat
  failing to print not matched lines from second file tester_V 14 8,798 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Print to a New Line when Appending File DaveG 0 1,665 Mar-30-2022, 04:14 AM
Last Post: DaveG
Sad Want to Save Print output in csv file Rasedul 5 15,272 Jan-11-2022, 07:04 PM
Last Post: snippsat
  Convert legacy print file to XLSX file davidm 1 2,465 Oct-17-2021, 05:08 AM
Last Post: davidm
  Why it does not print(file.read()) Rejaul84 1 3,205 Jul-01-2021, 10:37 PM
Last Post: bowlofred
  get two characters, count and print from a .txt file Pleiades 9 4,937 Oct-05-2020, 09:22 AM
Last Post: perfringo
  Cannot redirect print to a file tester_V 3 3,239 Sep-11-2020, 12:21 AM
Last Post: tester_V
  Print/write to file function tpolim008 4 3,856 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  Print date, Time and output to file tpolim008 3 3,196 Mar-26-2020, 06:49 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

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