Problem with String lib

This is the place for queries that don't fit in any of the other categories.

Problem with String lib

Postby Daniel Hadad » Fri Mar 08, 2013 1:41 am

Hi, i had been doing some MIT opencourseware tutorials on Python.
The goal was to open a .txt file with thousands of words, convert this into a string and split it into a list, with
each element being a word. The remaining part of the tutorial was to write a hangman game using random lib.
Pretty easy, ok. But...

If I input the code into Python's shell, line by line, it does works all right.
However, If i write it down and then run the module, it gives me:
"AttributeError: 'module' object has no attribute 'readline'" or any other function i have been trying to call up,
although string lib has been sucessfully imported.
The random lib works ok, though.

I'm pretty sure there are no other file at the same folder, should you suggest this. and I'm using 3.3.0.
- Thanks for the help.
Daniel Hadad
Posts: 1
Joined: Fri Mar 08, 2013 1:25 am

Re: Problem with String lib

Postby metulburr » Fri Mar 08, 2013 3:45 am

please give the full traceback, and a short code snippet of what you wrote to get that error.

Also the string module is "outdated", no one uses it except to get string constants.
we will be moving to on October 1 2016
more details here
User avatar
Posts: 2244
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Problem with String lib

Postby ichabod801 » Fri Mar 08, 2013 2:27 pm

readline isn't an attribute of the string module, it's a method of an open file. You want something like:

Code: Select all
text_file = open('path/to/file.txt')

Although it's generally more pythonic to just do a for loop over the file:

Code: Select all
for line in open('path/to/file.txt'):
    # do something with line
Due to the reasons discussed here we will be moving to on October 1st, 2016.
This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
Posts: 688
Joined: Sat Feb 09, 2013 12:54 pm
Location: Outside Washington DC

Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 5 guests