File Management and Directories
The files and directories to be backed up are specified in a list.
From Idea to Output
Running a Python program is not one single action. Your program moves through several states: you type code in an editor, save that code as a file, run the file with the Python command, and receive output. Understanding these transitions makes the save-and-run workflow easier to follow and easier to troubleshoot.
The Four-Step Workflow
The standard procedure for writing a Python program has four steps. First, open an editor. Second, type your code. Third, save the code as a file with the .py extension. Fourth, run the file using the python command. The order matters because the interpreter needs a saved file to read and execute.
What Saving Changes
Before saving, the code exists only in the editor's memory. It is therefore temporary: if the computer crashes before the save, that code is lost. Saving writes the code to disk as a persistent file. This is why saving is not merely a formality. It changes the location and durability of the program.
A Greeting Program’s Journey
Trace a small program intended to greet someone by name from editing through execution.
- 1Open an editor
The editor provides the place where the program will be written.
- 2Type the greeting code
The code is currently held in the editor's memory. It has not yet become a persistent file.
- 3Save with a .py extension
The greeting program is written to disk as a Python script. The .py extension identifies it as a Python file.
- 4Run with the python command
The interpreter reads the saved file, executes its instructions, and produces the greeting output.
The program moves from temporary editor code to a persistent Python file, then to execution and produced output.
Names, Extensions, and Directories
A Python script uses the .py file extension. This extension is the standard convention for Python scripts and signals that the file contains Python code. The run command must identify the correct script, and the filename used must match the saved file exactly. The command also needs to be run from the directory where the file can be found. A correct extension, matching filename, and correct directory work together.
Treat the saved filename and the directory as part of the run procedure. Before using the python command, confirm that the file was saved with .py and that the command refers to that same filename from the directory where the file is located.
Troubleshooting the Workflow
Common mistake Forgetting to save before running
What it looks likeThe newest code is visible in the editor, but the saved file still contains the earlier version.
Why it failsThe Python interpreter reads the file from disk, not unsaved editor changes.
Do this insteadSave the program before using the python command.
Common mistake Using the wrong file extension
What it looks likeThe script is saved without the .py extension.
Why it failsThe .py extension is the standard convention that identifies a Python script.
Do this insteadSave the program with the .py extension.
Common mistake Running from the wrong directory
What it looks likeThe saved file is in one directory, but the command is run from another directory.
Why it failsThe command cannot use the intended file if it is not being run from the directory where that file can be found.
Do this insteadRun the command from the directory containing the saved script.
Common mistake Mistyping the python command or filename
What it looks likeThe command does not use the correct Python command or does not match the saved filename.
Why it failsThe interpreter command and filename must be correct and match the saved script.
Do this insteadCheck the command, filename, and .py extension carefully.
Workflow Check
A greeting program is visible in an editor. The learner runs the python command immediately, discovers that the intended file cannot be used, and then notices that the program was saved without the .py extension. Identify the workflow problems and state the corrected order of actions.
Hints
- Start with the four standard steps.
- Check whether the code has been written to disk.
- Check the filename extension and the directory before running.
Corrected Order
Repair the greeting program's save-and-run workflow.
- 1Open the editor and type the code
The program begins as code in editor memory.
- 2Save the program
Write the code to disk using the .py extension.
- 3Confirm the directory
Make sure the run command is being used from the directory where the saved file can be found.
- 4Run the matching file
Use the python command with the correct saved filename and allow the interpreter to execute the file.
The corrected workflow is editor, code, saved .py file, correct directory, matching python command, and output.
Reliable Execution
- 1A dependable Python workflow follows four steps: open an editor, type code, save it as a .py file, and run it with the python command. Saving changes code from temporary editor memory into a persistent file on disk. Running changes the saved file into an executing program that produces output. When something goes wrong, check whether the file was saved, whether it has the .py extension, whether the command is running from the correct directory, and whether the python command and filename match exactly.
Key Takeaways
- The standard procedure is open an editor, type code, save as a .py file, and run with the python command.
- Unsaved code exists only in editor memory and can be lost before it is written to disk.
- The .py extension identifies the file as a Python script.
- The saved filename, command, and directory must correspond so the interpreter can find and execute the intended file.
- Common workflow errors include forgetting to save, using the wrong extension, running from the wrong directory, and mistyping the command.