I have been trying to learn Python for a while now. By chance, I happened across chapter 6 of the official tutorial through a Google search link pointing here. When I learned, from that page, that functions were the heart of modules, and that modules could be called from the command line, I was all ears. Here's my first attempt at doing both, openbook.py
import nltk, re, pprint
from __future__ import division
def openbook(book):
file = open(book)
raw = file.read()
tokens = nltk.wordpunct_tokenize(raw)
text = nltk.Text(tokens)
words = [w.lower() for w in text]
vocab = sorted(set(words))
return vocab
if __name__ == "__main__":
import sys
openbook(file(sys.argv[1]))
What I want is for this function to be importable as the module openbook, as well as for openbook.py to take a file from the command line and do all of those things to it.
When I run openbook.py from the command line, this happens:
gemeni@a:~/Projects-FinnegansWake$ python openbook.py vicocyclometer
Traceback (most recent call last):
File "openbook.py", line 23, in <module>
openbook(file(sys.argv[1]))
File "openbook.py", line 5, in openbook
file = open(book)
When I try using it as a module, this happens:
>>> import openbook
>>> openbook('vicocyclometer')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
So, what can I do to fix this, and hopefully continue down the long winding path to enlightenment?