How to Create a Python File

This guide shows how to create a .py file and run it on Linux, Windows, and macOS.

🔹 On Linux

  1. Open the Terminal (Ctrl + Alt + T).
  2. Navigate to the folder where you want to create the file:
    cd ~/Documents
  3. Create a new Python file using a text editor:
    nano hello.py
  4. Type your Python code:
    print("Hello, Linux!")
  5. Save and exit:
    • Press Ctrl + X
    • Press Y to confirm
    • Press Enter to save
  6. Run the Python file:
    python3 hello.py

🔹 On Windows

  1. Open Notepad or any text editor.
  2. Type your Python code:
    print("Hello, Windows!")
  3. Go to File > Save As
  4. Save the file with a .py extension:
    • File name: hello.py
    • Save as type: All Files
    • Encoding: UTF-8
  5. Open Command Prompt (Win + R → type cmd).
  6. Navigate to the folder:
    cd Desktop
  7. Run the Python file:
    python hello.py

🔹 On macOS

  1. Open Terminal (Cmd + Space → search for "Terminal").
  2. Navigate to a folder:
    cd ~/Documents
  3. Create a Python file using a text editor (like nano or vim):
    nano hello.py
  4. Type your code:
    print("Hello, macOS!")
  5. Save and exit:
    • Press Ctrl + X
    • Then Y and Enter
  6. Run the script:
    python3 hello.py

Notes