I had a wild time with data handling yesterday—so wild, in fact, that I had to cut some of the stuff out of the dump to fit it in this post. As I learned to format output with the % operator, and from that learned how to right-justify numbers with the print() statement, I went crazy-pants and started using for loops to build times tables that got into the hundreds and thousands just for grins and giggles. However, when I copy/pasted the logs of all that fun this article, Substack warned me that the article was too large for an email. Besides, we did that together here already, so I trimmed all that stuff out and will just pass on the formatted file access parts.
The log below gets into my first experiences with the meat and potatoes of directly writing to and reading from files. Again (sigh), I struggled through the tutorial’s Python 2 code example, researching and converting it into Python 3 as I encountered issues. I even got into pickling, which is cool, but I don’t know where I’d use it except for application installs.
After that, I tried error exception handling and threw in the towel on that for now. The differences between Python 2 and 3 in that area became too excessively annoying and time-consuming to deal with. I didn’t include that log here; it was just too messy. I’ll look for a specific Python 3 tutorial on exception handling and then give you my first experience with that.
I’ll spend several days now defining object classes and learning details about built-in ones. Later, I’ll look for specific modules with higher-level data handling object classes and Python best practices, learn about them, and use them in an application. Hopefully, I’ll find an SQL module I can access through Python. I don’t doubt that I’ll find one.
Here is my fun log for yesterday.
>>> for x in range(1,21):
... for y in range(1,21):
... mytable.append(str(x*y).rjust(4))
... print(str(x*y).rjust(4), end=" ")
... print()
... mytable[x].append()
...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Traceback (most recent call last):
File "<python-input-59>", line 6, in <module>
mytable[x].append()
^^^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute 'append'
>>> for x in range(1,21):
... for y in range(1,21):
... mytable.append(str(x*y).rjust(4))
... print(str(x*y).rjust(4), end=" ")
... print()
... mytable(x).append()
...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Traceback (most recent call last):
File "<python-input-60>", line 6, in <module>
mytable(x).append()
~~~~~~~^^^
TypeError: 'list' object is not callable
>>> for x in range(1,21):
... for y in range(1,21):
... mytable.append(str(x*y).rjust(4))
... print(str(x*y).rjust(4), end=" ")
... print()
... mytable(x,y).append()
...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Traceback (most recent call last):
File "<python-input-61>", line 6, in <module>
mytable(x,y).append()
~~~~~~~^^^^^
TypeError: 'list' object is not callable
>>> exit
PS C:\Users\WHousley> python
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb 4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> for x in range(1,11):
... print( '%2d %3d %4d' % (x,x*x,x*x*x))
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9 81 729
10 100 1000
>>> bob='a','b','c','d','e','f','g'
>>> bob
('a', 'b', 'c', 'd', 'e', 'f', 'g')
>>> for x in bob:
... print( '%-10s % x, end=".")
...
File "<python-input-3>", line 2
print( '%-10s % x, end=".")
^
SyntaxError: unterminated string literal (detected at line 2)
>>> for x in bob:
... print( '%-10s' % x, end=".")
...
>>> for x in bob: .c .d .e .f .g .
... print( '%-10s' % x, end=".")
... print()
...
a .b .c .d .e .f .g .
>>> name=('Alice','bob','George','Harold')
>>> age=(30,40,50,60)
>>> name=('Alice','Bob','George','Harold')
>>> age=(30,40,50,60)
>>> for x in len(name)
... print( '%-10s' % name(x), '%5d' % age(x), end=".")
... print()
...
File "<python-input-10>", line 1
for x in len(name)
^
SyntaxError: expected ':'
>>> for x in range(0,len(name))
... print( '%-10s' % name(x), '%5d' % age(x), end=".")
... print()
...
File "<python-input-11>", line 1
for x in range(0,len(name))
^
SyntaxError: expected ':'
>>> for x in range(0,len(name)):
... print( '%-10s' % name(x), '%5d' % age(x), end=".")
... print()
...
Traceback (most recent call last):
File "<python-input-12>", line 2, in <module>
print( '%-10s' % name(x), '%5d' % age(x), end=".")
~~~~^^^
TypeError: 'tuple' object is not callable
>>> name
('Alice', 'Bob', 'George', 'Harold')
>>> name(1)
Traceback (most recent call last):
File "<python-input-14>", line 1, in <module>
name(1)
~~~~^^^
TypeError: 'tuple' object is not callable
>>> name[1]
'Bob'
>>> for x in range(0,len(name)):
... print( '%-10s' % name[x], '%5d' % age[x], end=".")
... print()
...
Alice 30.Bob 40.George 50.Harold 60.
>>> for x in range(0,len(name)):
... print( '%-10s' % name[x], '%5d' % age[x], end="")
... print()
...
Alice 30Bob 40George 50Harold 60
>>> open("NamesAndAges.db" as database)
File "<python-input-18>", line 1
open("NamesAndAges.db" as database)
^^
SyntaxError: invalid syntax
>>> open("NamesAndAges.db", "a") as database
File "<python-input-19>", line 1
open("NamesAndAges.db", "a") as database
^^
SyntaxError: invalid syntax
>>> with open("NamesAndAges.db", "a") as database
File "<python-input-20>", line 1
with open("NamesAndAges.db", "a") as database
^
SyntaxError: expected ':'
>>> with open("NamesAndAges.db", "a") as database:
... for x in range(0,len(name)):
... database.write( '-10s' % name[x], '%5d' % age[x])
...
Traceback (most recent call last):
File "<python-input-21>", line 3, in <module>
database.write( '-10s' % name[x], '%5d' % age[x])
~~~~~~~^~~~~~~~~
TypeError: not all arguments converted during string formatting
>>> with open("NamesAndAges.db", "a") as database:
... for x in range(0,len(name)):
... database.write( '%-10s' % name[x], '%5d' % age[x])
...
Traceback (most recent call last):
File "<python-input-22>", line 3, in <module>
database.write( '%-10s' % name[x], '%5d' % age[x])
~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: TextIOWrapper.write() takes exactly one argument (2 given)
>>> with open("NamesAndAges.db", "a") as database:
... for x in range(0,len(name)):
... database.write( '%-10s%5d' % (name[x], age[x])
... print('Done')
...
...
...
File "<python-input-23>", line 3
database.write( '%-10s%5d' % (name[x], age[x])
^
SyntaxError: '(' was never closed
>>> with open("NamesAndAges.db", "a") as database:
... for x in range(0,len(name)):
... database.write( '%-10s%5d' % (name[x], age[x]))
... print('Done')
...
Done
>>> with open("NamesAndAges.db", "a") as database:
... for x in range(0,len(name)):
... database.write( '%-10s%5d' % (name[x], age[x]))
... print('Done')
...
Done
>>>
>>>
>>> exit
PS C:\Users\WHousley> python
Python 3.13.2 (tags/v3.13.2:4f8bb39, Feb 4 2025, 15:23:48) [MSC v.1942 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> with open("NamesAndAges.db", "a") as database:
... for x in range(0,len(name)):
... database.write( '%-10s%5d' % (name[x], age[x])
... print('Done')
...
File "<python-input-0>", line 3
database.write( '%-10s%5d' % (name[x], age[x])
^
SyntaxError: '(' was never closed
>>> ))))
File "<python-input-1>", line 1
))))
^
SyntaxError: unmatched ')'
>>> with open("NamesAndAges.db", "a") as database:
... for x in range(0,len(name)):
... database.write( '%-10s%5d' % (name[x], age[x])
... print('Done')
...
File "<python-input-2>", line 3
database.write( '%-10s%5d' % (name[x], age[x])
^
SyntaxError: '(' was never closed
>>> ))))
File "<python-input-3>", line 1
))))
^
SyntaxError: unmatched ')'
>>> database=open('database.csv',"r")
Traceback (most recent call last):
File "<python-input-4>", line 1, in <module>
database=open('database.csv',"r")
FileNotFoundError: [Errno 2] No such file or directory: 'database.csv'
>>> database=open('database',"r")
Traceback (most recent call last):
File "<python-input-5>", line 1, in <module>
database=open('database',"r")
FileNotFoundError: [Errno 2] No such file or directory: 'database'
>>> database=open('NamesAndAges.csv',"r")
>>> print f
File "<python-input-7>", line 1
print f
^^^^^^^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
>>> print (f)
Traceback (most recent call last):
File "<python-input-8>", line 1, in <module>
print (f)
^
NameError: name 'f' is not defined
>>> print (database)
<_io.TextIOWrapper name='NamesAndAges.csv' mode='r' encoding='cp1252'>
>>> database.read()
'Bob,13\nSally,15\nGeorge,16\nHarold,63\nGrace,35\nTerrance,12\nFerrin,43\n'
>>> database.readline()
''
>>> database.readline()
''
>>> database.readlines()
[]
>>> database=open('NamesAndAges.csv',"r")
>>> database.readlines()
['Bob,13\n', 'Sally,15\n', 'George,16\n', 'Harold,63\n', 'Grace,35\n', 'Terrance,12\n', 'Ferrin,43\n']
>>> database.readline()
''
>>> database.readline()
''
>>> database.readline(1)
''
>>> database.readline(2)
''
>>> database.readlines()
[]
>>> database.seek(0)
0
>>> database.readlines()
['Bob,13\n', 'Sally,15\n', 'George,16\n', 'Harold,63\n', 'Grace,35\n', 'Terrance,12\n', 'Ferrin,43\n']
>>> database.seek(0)
0
>>> database.readline(2)
'Bo'
>>> database.readline(2)
'b,'
>>>
>>> database.readline(2)
'13'
>>> database.seek(0)
0
>>> database.readline()
'Bob,13\n'
>>> database.seek(0)
0
>>> x=database.readline()
>>> x(1:-2)
File "<python-input-32>", line 1
x(1:-2)
^
SyntaxError: invalid syntax
>>> x(1:-2)
File "<python-input-33>", line 1
x(1:-2)
^
SyntaxError: invalid syntax
>>> x(1)
Traceback (most recent call last):
File "<python-input-34>", line 1, in <module>
x(1)
~^^^
TypeError: 'str' object is not callable
>>> x[1]
'o'
>>> x[0]
'B'
>>> repr(x)
"'Bob,13\n'"
>>> repr(x)
"'Bob,13\n'"
>>> y=repr(x)
>>> y
"'Bob,13\n'"
>>> database.tell('Grace')
Traceback (most recent call last):
File "<python-input-41>", line 1, in <module>
database.tell('Grace')
~~~~~~~~~~~~~^^^^^^^^^
TypeError: TextIOWrapper.tell() takes no arguments (1 given)
>>> database.tell()
8
>>> database.seek(0)
0
>>> database.tell()
0
>>> f.close
Traceback (most recent call last):
File "<python-input-45>", line 1, in <module>
f.close
^
NameError: name 'f' is not defined
>>> database.close()
>>> atoi("123")
Traceback (most recent call last):
File "<python-input-47>", line 1, in <module>
atoi("123")
^^^^
NameError: name 'atoi' is not defined
>>> x='54325432543'
>>> x.atoi()
Traceback (most recent call last):
File "<python-input-49>", line 1, in <module>
x.atoi()
^^^^^^
AttributeError: 'str' object has no attribute 'atoi'
>>> x.int()
...
...
Traceback (most recent call last):
File "<python-input-50>", line 1, in <module>
x.int()
^^^^^
AttributeError: 'str' object has no attribute 'int'
>>> x.int()
Traceback (most recent call last):
File "<python-input-51>", line 1, in <module>
x.int()
^^^^^
AttributeError: 'str' object has no attribute 'int'
>>> int(x)
54325432543
>>> x="Alice,34"
>>> int(x)
Traceback (most recent call last):
File "<python-input-54>", line 1, in <module>
int(x)
~~~^^^
ValueError: invalid literal for int() with base 10: 'Alice,34'
>>> x='54325432543'
>>> x='Alioce,34'
>>> int(x)
Traceback (most recent call last):
File "<python-input-57>", line 1, in <module>
int(x)
~~~^^^
ValueError: invalid literal for int() with base 10: 'Alioce,34'
>>> f=open('NamesAndAges.csv',"r")
>>> f.close
<built-in method close of _io.TextIOWrapper object at 0x000001A1940419A0>
>>> f=open('NamesAndAges.csv',"w")
>>> pickle.__doc__
Traceback (most recent call last):
File "<python-input-61>", line 1, in <module>
pickle.__doc__
^^^^^^
NameError: name 'pickle' is not defined. Did you forget to import 'pickle'?
>>> import pickle
>>> pickle.__doc__
'Create portable serialized representations of Python objects.\n\nSee module copyreg for a mechanism for registering custom picklers.\nSee module pickletools source for extensive comments.\n\nClasses:\n\n Pickler\n Unpickler\n\nFunctions:\n\n dump(object, file)\n dumps(object) -> string\n load(file) -> object\n loads(bytes) -> object\n\nMisc variables:\n\n __version__\n format_version\n compatible_formats\n\n'
>>> pickle(x,f)
Traceback (most recent call last):
File "<python-input-64>", line 1, in <module>
pickle(x,f)
~~~~~~^^^^^
TypeError: 'module' object is not callable
>>> pickle.dump.__doc__
'Write a pickled representation of obj to the open file object file.\n\nThis is equivalent to ``Pickler(file, protocol).dump(obj)``, but may\nbe more efficient.\n\nThe optional *protocol* argument tells the pickler to use the given\nprotocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default\nprotocol is 4. It was introduced in Python 3.4, and is incompatible\nwith previous versions.\n\nSpecifying a negative protocol version selects the highest protocol\nversion supported. The higher the protocol used, the more recent the\nversion of Python needed to read the pickle produced.\n\nThe *file* argument must have a write() method that accepts a single\nbytes argument. It can thus be a file object opened for binary\nwriting, an io.BytesIO instance, or any other custom object that meets\nthis interface.\n\nIf *fix_imports* is True and protocol is less than 3, pickle will try\nto map the new Python 3 names to the old module names used in Python\n2, so that the pickle data stream is readable with Python 2.\n\nIf *buffer_callback* is None (the default), buffer views are serialized\ninto *file* as part of the pickle stream. It is an error if\n*buffer_callback* is not None and *protocol* is None or smaller than 5.'
>>> pickle.dump.(x,f)
File "<python-input-66>", line 1
pickle.dump.(x,f)
^
SyntaxError: invalid syntax
>>> pickle.dump(x,f)
Traceback (most recent call last):
File "<python-input-67>", line 1, in <module>
pickle.dump(x,f)
~~~~~~~~~~~^^^^^
TypeError: write() argument must be str, not bytes
>>> pickle.load(x,f)
Traceback (most recent call last):
File "<python-input-68>", line 1, in <module>
pickle.load(x,f)
~~~~~~~~~~~^^^^^
TypeError: load() takes exactly 1 positional argument (2 given)
>>> f.close
<built-in method close of _io.TextIOWrapper object at 0x000001A194042180>
>>> data= {"name": "Alice", "age": 30, "scores": [85, 90, 95]}
>>> with open("datapickle.txt", "wb") as f:
... pickle.dump(data,f)
...
>>> with open("datapickle.txt", "wb") as f:
... pickle.load(datab,f)
...
Traceback (most recent call last):
File "<python-input-72>", line 2, in <module>
pickle.load(datab,f)
^^^^^
NameError: name 'datab' is not defined. Did you mean: 'data'?
>>> with open("datapickle.txt", "wb") as f:
... x=pickle.load(f)
...
Traceback (most recent call last):
File "<python-input-73>", line 2, in <module>
x=pickle.load(f)
io.UnsupportedOperation: read
>>> x
'Alioce,34'
>>> with open("datapickle.txt", "wb") as f:
... x=pickle.load(f)
...
Traceback (most recent call last):
File "<python-input-75>", line 2, in <module>
x=pickle.load(f)
io.UnsupportedOperation: read
>>> with open("datapickle.txt", "rb") as f:
... loaded_data=pickle.load(f)
...
Traceback (most recent call last):
File "<python-input-76>", line 2, in <module>
loaded_data=pickle.load(f)
EOFError: Ran out of input
>>> with open("datapickle.txt", "rb") as f:
... loaded_data=pickle.load(f)
...
Traceback (most recent call last):
File "<python-input-77>", line 2, in <module>
loaded_data=pickle.load(f)
EOFError: Ran out of input
>>> with open("datapickle.txt", "wb") as f:
... pickle.dump(data,f)
...
>>> with open("datapickle.txt", "rb") as f:
... loaded_data=pickle.load(f)
...
>>> loaded_data
{'name': 'Alice', 'age': 30, 'scores': [85, 90, 95]}