8.9. Debugging¶
When you are reading and writing files, you might run into problems with whitespace. These errors can be hard to debug because spaces, tabs, and newlines are normally invisible:
The built-in function repr
can help. It takes any object as
an argument and returns a string representation of the object. For
strings, it represents whitespace characters with backslash sequences:
This can be helpful for debugging.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
One other problem you might run into is that different systems use
different characters to indicate the end of a line. Some systems use a
newline, represented \n
. Others use a return character, represented
\r
. Some use both. If you move files between different systems, these
inconsistencies might cause problems.
- \n
- This is the symbol for a newline.
- \t
- This is the symbol for a tab.
- \r
- This is the symbol for the return character.
- \'
- This is the symbol for the ' character in a string.
Q-4: Which of the following symbols create newlines, or returns, in files? Select all that apply.
For most systems, there are applications to convert from one format to another. You can find them (and read more about this issue) at wikipedia.org/wiki/Newline. Or, of course, you could write one yourself.