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: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.
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.
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.
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.
You can save these changes with your system’s standard keyboard shortcut, or you can select
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.
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.
Become a Member to join the conversation.