Organizing Your Python Projects
A Python source file must be saved with the .py extension (for example, hello.py) so that the operating system and Python interpreter recognize it as a Python program.
From Temporary Code to Saved Program
Code written in an interactive shell or notebook can run immediately, but it disappears when you close the program. If you want to keep the code, reuse it, or share it with others, save it as a file on your computer. A saved Python file is a permanent record of the program that you can open, edit, and run whenever you need it.
Saving is not just an editor action. It creates a file in a folder on your computer, allowing the program to remain available after the editor is closed.
The Filename Python Recognizes
A Python source filename has two essential parts: a name you choose and the required .py extension. In hello.py, hello is the chosen name and .py is the extension. The extension tells the operating system and text editors that the file contains Python code, and it allows the Python interpreter to recognize the file as a Python program.
| Filename part | Purpose |
|---|---|
| hello | The name chosen for the program |
| .py | The required extension identifying Python code |
The two parts of the filename hello.py
Writing and Saving the File
Saving a Small Python Program
You have written a program in a text editor and want to keep it as a Python source file.
Choose the editor: Use a plain-text editor to write and save the code. Avoid word processors because they add formatting that Python cannot interpret.
Choose a filename: Give the file a meaningful name and add the required .py extension. For example, greet_user.py uses a chosen name followed by .py.
Choose a location: Save the file in the folder you selected for your Python programs.
Save the code: When you click Save, the code in the editor is written to computer storage as the file with the name and location you specified.
The saved file exists on the computer as a Python source file. It can be opened, edited, and executed by the Python interpreter.
The saved file is independent of the text editor. After saving, you can close the editor or turn off the computer, and the file remains in the chosen location exactly as it was saved. The editor is where you write and change the code; the file is the stored record of that code.
A Folder for Your Programs
Python files can be saved in any folder as long as you know where that folder is located. A practical approach is to create one dedicated folder for your Python programs, such as Python_Programs or My_Python_Code, on the Desktop or in the Documents folder. Saving your programs there keeps related work in one place and makes the files easier to find and run later.
Checking the Saved Result
- Look in the folder you intended to use.
- Confirm that the file has the name you chose.
- Confirm that the filename ends with .py.
- Remember that the file should contain the code you saved from the text editor.
Verification connects the filename, location, and contents. The operating system creates the file using the name you specified and stores it in the folder you chose. The .py extension is part of that filename, while the code you typed is the content stored in the file.
Saving the program without the .py extension
The required extension is missing, so the operating system and Python interpreter do not recognize the file as a Python source file.
Fix:
Include .py at the end of the filename.Writing Python code in a word processor
Word processors add formatting that Python cannot interpret.
Fix:
Use a plain-text editor to write and save the code.Scattering programs across unrelated folders
The programs become harder to find and manage as you write more of them.
Fix:
Create one dedicated folder and save your Python programs there.Closing an interactive shell without saving the code
That code disappears when the program is closed.
Fix:
Save code you want to keep as a .py file.
Practice the Workflow
Imagine you have written a Python program that greets a user. Describe how you would save it so that you can find and reuse it later.
Hints
- Name the type of editor you would use.
- Include the required filename extension.
- Choose a dedicated folder for your Python programs.
- Explain what happens to the code when you save.
Checking a Proposed Filename
Decide whether my_program.py is suitable as a Python source filename.
Inspect the name: my_program is a meaningful name selected for the program.
Inspect the extension: The filename ends in .py, which is the required extension for a Python source file.
Check the location: The file should be saved in the dedicated folder chosen for Python programs.
my_program.py has the required filename structure. If it is saved in the intended folder using a plain-text editor, it is organized as a Python source file.
Essential Saving Rules
- Use a plain-text editor, not a word processor, for Python code.
- Choose a meaningful filename and end it with the required .py extension.
- Use underscores instead of spaces and avoid special characters in filenames.
- Store your Python programs in one dedicated folder that you can locate easily.
- Saving writes the code from the editor to a permanent file on computer storage.
Key Takeaways
- A Python source file needs a name and the .py extension.
- The .py extension helps the operating system and Python interpreter recognize the file as Python code.
- Use a plain-text editor and save programs in a dedicated folder.
- Saving creates a persistent file containing the code written in the editor.
- Verify both the filename and the folder after saving.