2

I'm fairly new to programming/coding, so bear with me.

I noticed that when I run .py files in cmd, the command looks something like:

C:\Users\Desktop\hello

"hello"

with hello being the python file, and the hello string being output.

For my friend, who's learning code along side me, the command looks more like:

C:\Users\Desktop\python hello.py

"hello"

My question is, what is the explanation for why my command is shorter as opposed to my friends? Can I help get my friends commands to look more like mine? I would really love some clarity here. Thanks!

5
  • It is not about shorter or longer command. It is more of folder path where the hello python file was created. Commented Jan 23, 2021 at 6:58
  • Thanks for your reply. Can you explain a little more about why I'm able to omit the "python" and ".py" portions of my command as opposed to my friend? We can both be virtually in the same directory (on different pcs) and he isn't able to input the shorter command I showed. Commented Jan 23, 2021 at 7:01
  • As Sunilbaba said, Please check your folder/dir structure. Commented Jan 23, 2021 at 7:02
  • Type ftype /? and assoc /?. Then path /?. Commented Jan 23, 2021 at 7:20
  • And set pathext. Commented Jan 23, 2021 at 7:21

2 Answers 2

1

The Python installer for Windows has the option to register file extensions. If registered, python is not required to be typed, just the extension, like so:

c:\> script.py

The PATHEXT environment variable has a list of extensions to try if not present on a command. It typically looks like:

C:\>set pathext
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

The installer doesn't update this, but if you edit and add typical Python extensions you don't have to type the extension. To find the editor in Windows 10, search for "edit environment" in the taskbar.

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw

You can run some commands to see if Python is registered for Python scripts:

C:\>assoc .py
.py=Python.File

C:\>ftype Python.File
Python.File="C:\WINDOWS\py.exe" "%L" %*

Above shows that py.exe (the Python Launcher) is associated with the .py extension. It is another installer option that give more options for launching scripts, like specifying what version of python to run if you have more than one installed. See PyLauncher for more details.

Sign up to request clarification or add additional context in comments.

1 Comment

This was the most helpful answer. Thank you!
0

According to your example your just chose a longer name for their python file. yours = 'hello.py' friends = 'python hello.py'

inside each of the files you will find the same print statement that is actually responsible for the output

print("hello")

1 Comment

Incorrect. Both of our files are simply named hello, with a .py extension. Looks like Mark Tolonen gave me the answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.