Command prompt windows python

Execute a Command Prompt Command from Python

Data to Fish

Need to execute a Command Prompt command from Python?

If so, depending on your needs, you may use either of the two methods below to a execute a Command Prompt command from Python:

(1) CMD /K – execute a command and then remain:

import os os.system('cmd /k "Your Command Prompt Command"')

(2) CMD /C – execute a command and then terminate:

import os os.system('cmd /c "Your Command Prompt Command"')

Still not sure how to apply the above methods in Python?

Let’s then review few examples to better understand how to execute a Command Prompt command from Python.

Methods to Execute a Command Prompt Command from Python

Method 1 (CMD /K): Execute a command and then remain

To see how to apply the first method in practice, let’s review a simple example where we’ll execute a simple command in Python to:

  • Display the current date in the Command Prompt
  • The Command Prompt will remain opened following the execution of the command
Читайте также:  Java или c sharp

You may then apply the following code in Python to achieve the above goals:

import os os.system('cmd /k "date"')

Once you run the code in Python, you’ll get the date in the command prompt:

Now what if you want to execute multiple command prompt commands from Python?

If that’s the case, you can insert the ‘&’ symbol (or other symbols, such as ‘&&’ for instance) in between the commands.

For example, what if you want to display all the characters in the command prompt in green and display the current date?

You can then use the following syntax in Python:

import os os.system('cmd /k "color a & date"')

You’ll now see the current date displayed in green:

Note that for more complex commands, you may find it useful to run a batch file from Python.

Method 2 (CMD /C): Execute a command and then terminate

For this method, you can execute the same commands as reviewed under the first method, only this time the Command Prompt will be closed following the execution of the commands.

For example, you may apply the following code in Python to change the color of all characters to green:

import os os.system('cmd /c "color a"')

In this case, the command will still get executed, but you may not be able to see it on your monitor.

In general, you can get a useful legend with further information by typing the command below in the Command Prompt:

Источник

How to Use Windows Command Prompt to Run a Python File

This article was co-authored by wikiHow staff writer, Jack Lloyd. Jack Lloyd is a Technology Writer and Editor for wikiHow. He has over two years of experience writing and editing technology-related articles. He is technology enthusiast and an English teacher.

The wikiHow Tech Team also followed the article’s instructions and verified that they work.

This article has been viewed 781,269 times.

Whether you’re writing Python code on your Windows PC or just want to use existing Python scripts, it’ll be helpful to learn how to run code from the Command Prompt. Running Python code is easy—you’ll just need to have Python installed. This wikiHow article will walk you through opening a Python file from Command Prompt, and teach you how to fix the common «python is not recognized as an internal or external command» error.

Finding the Python File Path

Image titled Use Windows Command Prompt to Run a Python File Step 1

Image titled Use Windows Command Prompt to Run a Python File Step 2

Image titled Use Windows Command Prompt to Run a Python File Step 3

Image titled Use Windows Command Prompt to Run a Python File Step 4

Image titled Use Windows Command Prompt to Run a Python File Step 5

  • You can copy the location by highlighting it (click and drag your mouse across the «Location» value) and then pressing Ctrl + C .

Running a Python File

Image titled Use Windows Command Prompt to Run a Python File Step 6

Windows Start

Image titled Use Windows Command Prompt to Run a Python File Step 7

Image titled Use Windows Command Prompt to Run a Python File Step 8

Windows cmd

Image titled Use Windows Command Prompt to Run a Python File Step 9

  • For example, to open a Python file in a folder named «Files» on your Desktop, you would enter cd desktop/Files here.
  • If you copied the path to the file, you can type in cd and a space and then press Ctrl + V to paste in the path.

Image titled Use Windows Command Prompt to Run a Python File Step 10

  • For example, if your Python file is named «script», you would type in python script.py here.
  • If your Python file has one or more spaces in its name, you’ll place quotation marks around the file name and extension (e.g., python «my script.py» ).

Image titled Use Windows Command Prompt to Run a Python File Step 11

  • If you encounter an error that says ‘python’ is not recognized as an internal or external command after pressing Enter, you’ll need to add Python to the PATH list before retrying this part.

Adding Python to the PATH List

Image titled Use Windows Command Prompt to Run a Python File Step 12

Windows File Explorer

Image titled Use Windows Command Prompt to Run a Python File Step 13

  • Click This PC on the left side of the File Explorer.
  • Double-click your hard drive in the «Devices and drives» section.
  • Scroll down and double-click the «Users» folder.
  • Double-click the folder with your username on it.
  • Scroll down and double-click «AppData».
  • Double-click «Local».
  • Scroll down and double-click «Programs».
  • Double-click the «Python» folder.
  • Double-click the Python folder with your preferred version number (e.g., «Python36»).

Image titled Use Windows Command Prompt to Run a Python File Step 14

Copy the path to the Python folder. Click once the address bar at the top of the File Explorer to highlight its contents, then press Ctrl + C to copy the highlighted address.

Image titled Use Windows Command Prompt to Run a Python File Step 15

Windows Start

Image titled Use Windows Command Prompt to Run a Python File Step 16

Image titled Use Windows Command Prompt to Run a Python File Step 17

Click System info . This is a link in the upper-right corner of the window. Doing so opens the System Information window.

Image titled Use Windows Command Prompt to Run a Python File Step 18

Click the Advanced system settings link. You’ll see this in the upper-left side of the System Information window. Yet another window will pop up.

Image titled Use Windows Command Prompt to Run a Python File Step 19

Image titled Use Windows Command Prompt to Run a Python File Step 20

  • You may have to scroll up or down with your mouse cursor hovering over the «User variables» pane to find the «Path» variable.

Image titled Use Windows Command Prompt to Run a Python File Step 21

Image titled Use Windows Command Prompt to Run a Python File Step 22

Click New . It’s on the right side of the window. A text field will open in the middle of the window.

Image titled Use Windows Command Prompt to Run a Python File Step 23

Paste in your copied path. Press Ctrl + V to do so. Your copied path will appear in the text field in the middle of the window.

Image titled Use Windows Command Prompt to Run a Python File Step 24

Click OK on the three open windows. This will save your changes and close the «Path» window, the «Environmental Variables» window, and the «System Properties» window.

Community Q&A

I want to create a shortcut that executes the utility «ptpython,» running in the cmd prompt. The shortcut I have points to the directory containing «ptpython.exe» file but it does not execute it.

It sounds like ptpython.exe is a command-line utility, meaning it will only start if you execute it from a DOS window — you can’t create a shortcut for it directly. You can probably create a shortcut to cmd.exe, though (the DOS window) and pass it the ptpython.exe file as a parameter. Something like «cmd.exe /c ptpython.exe» should work, or if this disappears in the end, try with /k (instead of /c).

Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

Yes. The directions to access the environment variables would be slightly different, as there is no «Power User» menu in Windows 7. Instead: 1. Press the Windows key and R to open the Run dialog. 2. Enter «sysdm.cpl». 3. Click the «Advanced» tab of the System Properties Window. 4. Click the «Environmental variables». Most everything else would work as described even on Windows 95 (if there’s a version of Python for Windows 95).

Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

After opening the Command Prompt and navigation to the directory in which the py file exists and opening Python, not able to run the file using python file_name.py. It says that the syntax is wrong.

Источник

Folyamatosan induló kurzusaink

A CodeBerry megtanít a legmodernebb technológiák használatára, és megadja neked a szükséges tudást és eszközöket ahhoz, hogy fejlesztőként dolgozhass.

Készen állsz a tanulásra? Csatlakozz te is a 160 000 programozást tanuló diákunk csapatához!

Tanulj online

Tanulhatsz otthon, a szünetekben, vagy a kedvenc kávézódban.

Interaktív feladatok

A tudásszintedtől függetlenül több, mint 100 órányi szórakoztató feladatsor vár.

Előképzettség nélkül

Teljesen kezdőként is belevághatsz a kurzusokba, semmilyen programozási ismeretre nincs hozzá szükséged.

Örökös hozzáférés

A kurzusok elvégzése után is bármikor hozzáférhetsz a leckékhez, hogy ismételhess és gyakorolhass, ami a programozás tanulásánál különösen fontos.

Tapasztalt mentorok

A CodeBerry tanárai több év programozói tapasztalattal rendelkező szakemberek, akikre mindig számíthatsz, ha segítségre van szükséged a tanulás folyamán.

Pénzvisszafizetési garancia

Ha nem vagy elégedett a szolgáltatásunkkal, a vásárlástól számított 14 napon belül kérdés nélkül visszaadjuk a pénzed.

Источник

How To Execute Python Scripts In Command Prompt

How To Execute Python Scripts In Command Prompt by manish sharma

Execute Python Scripts In Command Prompt

In the previous tutorial we learnt how to create and execute a Python script using IDLE. But there are instances when we have to execute the scripts using command prompt. Thus, in this tutorial we will learn how to execute Python scripts using command prompt in windows 10.

Before starting anything let me quickly explain the setup we will be using for the demonstration. I have created a directory with the name “Python Tutorials” in my D drive. In that directory I have created a script with name “Tut1.py”. This script contains a code which on successful execution displays the current version of Python installed on my system.

How To Execute Python Scripts In Command Prompt by manish sharma

Now let’s move ahead and learn how to execute this script using command prompt in windows 10.

There are two ways of doing it:

  1. In your command prompt, navigate to the directory where your scripts are saved and then execute them using Python interpreter. Or
  2. Invoke the Python interpreter with the location of your script. The interpreter will use the location path to find the script and then execute it.

Let’s try to execute our script “Tut1.py” using both the above mentioned ways. Let’s start with the first way which is navigating to the directory.

Step 1: Navigating to the script location.

In order to execute your script using command prompt you first need to locate the script in window’s file system and then navigate your command prompt to its location. For example in my case the script is located at “D:/Python Tutorials/Tut1.py” so I need to navigate my command prompt to this location.

Command prompt tips

  • To change the drive in command prompt simply write the drive letter along with colon and hit enter. For example to change from C to D drive simply write D: and hit enter.
  • To change the current directory in command prompt we use “cd” command. For example to get into the directory “Python Tutorial” simply write “cd Python Tutorials”

How To Execute Python Scripts In Command Prompt by manish sharma

Step 2: Invoke the Python interpreter to run the script.

Python’s interpreter can be invoked simply by writing keyword “python” on command prompt. In order to execute a script we need to accompany the keyword “python” with the name of the script enclosed inside a pair of double quotes. Like this:

D:\Python Tutorials>python "tut1.py"

Info: Always take care of two things.

  1. Always enclose the name of the script inside a pair of double quotes and
  2. Add *.py file extension at the end of the name of the script.

These two rules are mandatory. You have to follow this otherwise you will get an error.

Invoke the Python interpreter with the script location

This method of executing Python script is easier than the previous one. In this method we invoke Python’s interpreter with the location of the script. Let’s once again run the same Python script “tut1.py”.

The location path of the “tut1.py” script is D:\Python Tutorials\tut1.py (this location could be different in your case.) In order to run this script we have to specify this location followed by the keyword Python in the command prompt. Like this

C:\>python "d:\Python Tutorials\tut1.py"

On pressing the enter key you will see the result if there is no error but if there is any then you will see the error stack on your screen.

The rules for running the script will remain the same.

  1. Always enclose the location of the script inside a pair of double quotes and
  2. Add *.py file extension at the end of the name of the script.

So these are the two different ways in which you can execute python scripts in command prompt in windows 10. Hope you will find this tutorial helpful. If so then I want you to share this blog on your social media. I will be really grateful. Thanks & have a great day!

Источник

Оцените статью