How to Create a Python File in Terminal

To create a Python file in a terminal, make a text file ending in .py, add Python code, save it, and run it with a Python interpreter. You can do this with a terminal editor such as Nano or Vim, with Notepad on Windows, or by writing a one-line script directly from the shell. The important parts are working in the right folder and using the Python command that matches your operating system.

Python Files and Terminal Basics

A Python script is a plain-text file with the .py extension, such as hello.py. The extension tells you, and tools such as editors and Python itself, that the file contains Python source code.

Before creating a file, open a terminal:

  • On Windows, search for cmd in the Start menu to open Command Prompt. The prompt often starts with something like C:\>, though the drive and folder can differ. The Python on Windows FAQ uses the same approach.
  • On macOS, open the Terminal app.
  • On Linux, open your distribution’s Terminal application.

On Windows, enter:

py --version

On macOS or Linux, enter:

python3 --version

If the command prints a Python version, you have an interpreter available. If your terminal shows a >>> prompt instead of a folder path, you are inside Python’s interactive interpreter rather than your normal shell.

How to Create a Python File in Terminal

Choose the route that matches the terminal and editor available on your computer.

Environment File-Creation Route Typical Way to Run the File
Windows Command Prompt or PowerShell Open the new file in Notepad Windows Python launcher
macOS or Linux Create and edit it with Nano Python 3 command
Any system with Vim Create and edit it with Vim Your installed Python command
Any shell Write a simple one-line script with shell output redirection Your installed Python command

Step 1: Create or Enter a Working Folder

Keep scripts in a dedicated folder instead of saving them in a random terminal location. From your current directory, create a folder named python-files and enter it.

In Windows Command Prompt or PowerShell:

mkdir python-files
cd python-files

On macOS or Linux:

mkdir -p python-files
cd python-files

If the folder already exists, skip the folder-creation command and enter it with the cd command.

Step 2: Create and Edit hello.py

On Windows, create the file and open it in Notepad:

notepad hello.py

If Notepad asks whether to create the file, choose Yes.

On macOS or Linux, create and open the file in Nano:

nano hello.py

Add this code to the file:

print("Hello, World!")

Step 3: Save and Close the Editor

In Notepad, select File > Save, then close the window. Make sure the filename remains hello.py, not hello.py.txt.

In Nano:

  1. Press Ctrl+O to save.
  2. Press Enter to keep the filename hello.py.
  3. Press Ctrl+X to exit Nano.

At this point, your terminal should again show its normal folder prompt.

Step 4: Run the Python File

On Windows, run the sample file with the Python launcher:

py hello.py

On macOS or Linux, run it with Python 3:

python3 hello.py

You should see:

Hello, World!

The general command form documented in the Python command-line documentation is:

python myscript.py

Use that form only when python on your system points to Python 3. If your macOS terminal reports Python 3.15 when you check its installed version, the version-specific form is:

python3.15 myscript.py

The exact version suffix depends on the Python version installed on your machine.

Step 5: Confirm the File Is in the Right Folder

If the script runs and prints Hello, World!, the file was created, saved, and executed successfully.

To list the files in your current folder on Windows, use:

dir

On macOS or Linux, use:

ls

Look for hello.py in the output before creating another file with the same name.

Create a Python File Without Opening an Editor

For a very small script, you can create the file and put code in it with one terminal command. This is useful for quick tests, but an editor is more practical once the script has multiple lines.

Windows Command Prompt

This creates hello.py with one print statement:

echo print("Hello, World!") > hello.py

Run it with:

py hello.py

Windows PowerShell

In PowerShell, use this command to write the sample code into hello.py: ‘print(“Hello, World!”)’ | Set-Content hello.py

'print("Hello, World!")' | Set-Content hello.py

Run it with:

py hello.py

macOS and Linux

On macOS and Linux, use this command to write the sample print statement and a newline to hello.py: printf ‘print(“Hello, World!”)\n’ > hello.py

printf 'print("Hello, World!")\n' > hello.py

Run it with:

python3 hello.py

The > character replaces the contents of an existing file with the same name. Use a new filename if you do not intend to overwrite a script.

Create an Empty Python File

Sometimes you only need an empty .py file to fill in later.

In Windows Command Prompt:

type nul > hello.py

In PowerShell:

New-Item -ItemType File hello.py

On macOS or Linux:

touch hello.py

An empty Python file runs without output because it contains no instructions. Open it in an editor, add code, save it, and run it when you are ready.

Run a Python File From Another Folder

You do not have to be inside the script’s folder if you provide the file path to Python. Put quotation marks around a path that contains spaces.

On Windows:

py "C:\path\to\your\hello.py"

On macOS or Linux:

python3 "/path/to/your/hello.py"

Entering only a script’s full file location can still fail if the terminal cannot find the Python interpreter or if the path is not quoted correctly. Calling the interpreter first, then providing the script path, avoids that problem.

Run a Unix Script Directly

On macOS and Linux, you can make a script act like a command-line program. Put this interpreter line at the very top of the file:

#!/usr/bin/env python3
print("Hello, World!")

Make the file executable:

chmod +x hello.py

Then run it from its folder:

./hello.py

The #!/usr/bin/env python3 line tells Unix-like systems to locate Python 3 when the script is started directly.

Create and Run Python Files in VS Code

A terminal editor is useful, but a code editor is usually more comfortable for larger scripts. In Microsoft’s Visual Studio Code, create a file from the Welcome page by selecting New File > Python file.

Save the file with a .py name, add your code, and select Run Python File in Terminal from the play button at the top-right of the editor. VS Code opens its integrated terminal and runs the active file with the selected Python interpreter.

For a quick test of only part of a script:

  1. Select one or more lines in the editor.
  2. Press Shift+Enter.

To open an interactive Python session in VS Code, open the Command Palette and select:

Python: Start REPL

VS Code needs a Python interpreter and the Python extension installed to provide these Python-specific features.

Confirm and Troubleshoot Python File Creation

Use this table when creating or running the file does not work as expected.

Symptom Likely Cause What to Check
“python is not recognized” or “command not found” Python is not installed, or the command name differs Try the Windows launcher or the Python 3 command for your operating system
“No such file” or “can’t open file” You are in the wrong folder or mistyped the filename List the folder contents and check the filename exactly
The file is named hello.py.txt The editor added a text-file extension Rename it so the name ends only in .py
You see >>> You are in Python’s interactive mode, not the system terminal Exit the interpreter, then run the script from the normal prompt
The script runs with no output The file has no print() call or other visible action Add a test print statement and save the file

If the Windows launcher does not work, check it again with:

py --version

If macOS or Linux cannot find Python 3, check with:

python3 --version

To leave the interactive Python interpreter, press Ctrl+D on Unix-like systems. On Windows, press Ctrl+Z, then press Enter.

A successful run from the folder containing the script is the simplest proof that the filename, working directory, and interpreter command all match.

Next, create a project folder for your scripts, keep each program in its own .py file, and run the file from that folder while you develop it.

Frequently Asked Questions

How Do I Create an Empty Python File in Windows Command Prompt?

Use this Windows Command Prompt command:

type nul > hello.py

It creates an empty file named hello.py in the current folder. Open it in Notepad, add Python code, save it, and run it with the Windows Python launcher.

How Do I Create a Python File in Linux Terminal?

Open Nano with the filename you want:

nano hello.py

Add Python code, save with Ctrl+O, press Enter, then exit with Ctrl+X. Run the completed file with:

python3 hello.py

How Do I Create a File From Python Code?

Use Python’s pathlib module to create or replace a text file with content:

from pathlib import Path

Path("hello.py").write_text('print("Hello, World!")\n', encoding="utf-8")

When that code runs, it creates hello.py in the current working directory. This approach is useful when one Python program needs to generate another Python file.

Can I Run a Python File by Double-Clicking It?

On Windows, .py files are commonly associated with Python, so double-clicking can run them. A console window may close immediately after the script finishes, which makes terminal execution easier for seeing output and errors.

On macOS and Linux, direct execution requires the interpreter line `#!/usr/bin/env python3`, executable permission from `chmod +x hello.py`, and running the file with `./hello.py`.

How Do I Exit Python Interactive Mode?

Press Ctrl+D on macOS and Linux. On Windows, press Ctrl+Z and then Enter.