Working with files can seem very normal from the perspective of a computer end-user. You double-click a program, you create a new file by selecting a context menu, and you're ready to start typing your novel:
Photo by Dan Counsell https://unsplash.com/@dancounsell
However, you've already learned that there are also programmatic ways to handle these familiar tasks. You did something similar when you learned how to move files with a Python script. Before that, you had probably only done that using your operating system's graphical user interface.
In this section, you'll keep looking at files from the perspective of a programmer. You'll think of them as a way to store information, and you'll use Python to write data to a file and read it, too.
Why Files Are Useful
In one of the projects in the previous course section, you counted the different types of files on your desktop. You might have gotten an output similar to this:
{'': 8, '.csv': 2, '.md': 2, '.png': 11}
Your count dictionary shows that you've got the following content cluttering your desktop:
- 8 folders (
''), they have no file extension - 2 CSV files (
.csv) - 2 Markdown files (
.md) - 11 screenshots or other images (
.png)
Now, that's some juicy information, but how will you keep track of it until the next time you run your script to analyze your desktop contents? Most likely, you'll have forgotten about it unless you write it down. Python can keep track of the data for you.
It can be extremely handy to use your computer's persistent memory to keep track of data across multiple runs of your scripts. The most widely known type of persistent memory is a file.
If you think of files from the perspective of a programmer, where they are just a place to persist some data across multiple executions of your programs, then you're opening up a whole new world. File extensions are just some additional information that gives your operating system a hint as to which program it should use to access the data saved in that file.
Keep that script you just wrote around, and get ready to adapt it in order to write your desktop analysis data to a new file.