How to Work With Python Files
00:00 How to Work With Python Files. Python IDLE offers a full-fledged file editor, which gives you the ability to write and execute Python programs from within this program. The built-in file editor also includes several features, such as code completion and automatic indentation, that will speed up your coding workflow. First, let’s take a look at how to write and execute programs in Python IDLE.
00:28 To start a new Python file, select File → New File from the menu. This will open a blank file in the editor, as seen on-screen. From this window, you can write a brand new Python file.
00:40 You can also open existing Python files by selecting File → Open in the menu. This will bring up your operating system’s file browser.
00:49 Then you can find the Python file you want to open.
00:54 If you’re interested in reading the source code for a Python module, then you can select File → Path Browser. This will let you view the modules that Python IDLE can see. When you double-click on one, the file editor will open it up, and you’ll be able to read it.
01:13 Once you’ve opened a file in IDLE, you can make changes to it. When you’re ready to edit a file, you’ll see something similar to what’s seen on-screen. The contents of the file are displayed in the open window.
01:25 The bar along the top of the window contains three pieces of important information:
01:32
the name of the file that you’re editing, the full path to the folder where you can find this file on your computer, and the version of Python that IDLE is using. On-screen, you can see the file is called myfile.py
, you can see the path of the file, and you can also see that the Python version is 3.10.0.
01:54 You may have noticed there are two numbers at the bottom-right corner of the window.
02:00
Ln
shows a line number that your cursor is on, and Col
shows the column number that your cursor is on. It’s useful to see these numbers so that you can find errors more quickly, and they also help you ensure your lines of code stay at an appropriate length.
02:15
There are a few visual clues in this window that will help you to remember to save your work. If you look closely, then you’ll see that Python IDLE will uses asterisks (*
) to let you know that your file has unsaved changes.
02:27
You can see that as soon as the print
statement is added, the file name shown in the top of the IDLE window is surrounded by asterisks. This means that there are unsaved changes in your editor.
02:37
You can save these changes with your system’s standard keyboard shortcut, or you can select File
→ Save
from the menu. Make sure that you save your file with a .py
extension so that syntax highlighting is enabled.
02:53 When you want to execute a file that you’ve created in IDLE, you should first make sure that it’s saved. Remember you can see if your file is properly saved by looking for asterisks around the file name at the top of the file editor window. Don’t worry if you forget, though.
03:08 Python IDLE will remind you to save whenever you attempt to execute an unsaved file. To execute a file in IDLE, simply press the F5 key on your keyboard. You can also select Run → Run Module from the menu.
03:22 Either option will restart the Python interpreter and then run the code that you’ve written with a fresh interpreter. The process is the same as when you run the command seen on-screen in your terminal. When your code is done executing, the interpreter will know everything about your code, including any global variables, functions, and classes.
03:43
This makes Python IDLE a great place to inspect your data if something goes wrong. If you ever need to interrupt the execution of your program, then you can press Ctrl+C
in the interpreter that’s running your code.
03:57 In the next section of the course, we’ll see how you can use Python IDLE’s features to speed up your coding and improve your workflow.
Become a Member to join the conversation.