APC Australia

Python — Part 6: Writing and reading files

Learn how to read, write and modify files — even hack WAV audio files — using the Python programmin­g language with Darren Yates.

-

Nothing gives you more power over a computer than being able to program it, and over the last few months, we’ve been looking at the basic nuts and bolts of the Python programmin­g language. This month, we add an important new tool into the coding toolbox — manipulati­ng files. Being able to read, write and modify files is one of the key functions within any programmin­g language — and the great news is that it’s easy to do in Python.

TWO TYPES OF FILES

There are two basic file types you’ll come across — text files and binary files. Text files are, obviously, full of text, but binary files are just about everything else, from images to video to audio to program executable­s. They can contain data stored from any datatype. In Python, it’s likely you’ll deal mostly with text files, but the language easily handles binary files, too — and this month, we’ll cover both file types.

READING TEXT FILES

Arguably the simplest file task to do in Python is read a text file — that’s because you pretty much know what to expect: just text. Python makes it easy to import text files into your programs with a selection of functions that load the text typically as a series of strings. Since the text is imported as a string datatype, you also have lots of useful functions for not only editing the string but converting it into other datatypes. We’ll start with a simple app this month, so open ‘textread.py’ into the Python IDE.

The code begins by writing a welcome message to the user, followed by asking for a filename. For simplicity, we’re loading text files direct from the textread.py source folder with no filepaths, so all you have to do is type in the filename. There’s a sample text file in that folder called ‘text.txt’. We’ve combined the input statement inside the file.open() function, opening the user’s file in ‘r’ (read) mode. After that, we print a blank line for tidiness, followed by reading in all of the file’s lines of text into the variable ‘textlines’ using the file.readlines() function. This initialise­s ‘textlines’ as a list.

From there, we initialise an integer variable, ‘wordcount’, with zero and then we iterate over the textlines list with a for-loop — first, we strip the excess carriage-return characters from each line of text, print the text to the screen and then count off the number of spaces in that line. This continues for each line in the ‘textlines’ list. To finish, we print the total word count stored in ‘wordcount’.

Note, if you try to load a non-text or non-existent file, the code will crash — yes, we could’ve enclosed the code in error-detection/correction statements, but for now, we’re just interested in how the basic file functions work — and as you can see, it doesn’t take much to read a text file!

WRITING TEXT FILES

But what about writing one? For this, we’ve create the world’s silliest word processor to show how file-writing works — it’ll open and read a text file, allow you to add text one line at a time and you enter a line with just ‘@’ to save and quit. Load up ‘textwrite.py’ into the Python

 ??  ?? This WAV audio header chart shows the parameters at the start of a WAV file.
This WAV audio header chart shows the parameters at the start of a WAV file.

Newspapers in English

Newspapers from Australia