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
- Open the Terminal (Ctrl + Alt + T).
- Navigate to the folder where you want to create the file:
cd ~/Documents
- Create a new Python file using a text editor:
nano hello.py
- Type your Python code:
print("Hello, Linux!")
- Save and exit:
- Press
Ctrl + X
- Press
Y
to confirm
- Press
Enter
to save
- Run the Python file:
python3 hello.py
🔹 On Windows
- Open Notepad or any text editor.
- Type your Python code:
print("Hello, Windows!")
- Go to File > Save As
- Save the file with a
.py
extension:
- File name:
hello.py
- Save as type:
All Files
- Encoding:
UTF-8
- Open Command Prompt (Win + R → type
cmd
).
- Navigate to the folder:
cd Desktop
- Run the Python file:
python hello.py
🔹 On macOS
- Open Terminal (Cmd + Space → search for "Terminal").
- Navigate to a folder:
cd ~/Documents
- Create a Python file using a text editor (like nano or vim):
nano hello.py
- Type your code:
print("Hello, macOS!")
- Save and exit:
- Press
Ctrl + X
- Then
Y
and Enter
- Run the script:
python3 hello.py
Notes
- Make sure Python is installed on your system. You can check with
python --version
or python3 --version
.
- Use
python
or python3
depending on your system setup.
- You can also use code editors like VS Code, Sublime Text, or PyCharm to create and run Python files.