How to call a file

Gary Herron gherron at islandtraining.com
Sat May 10 11:32:47 EDT 2008


Chris Sprinkles wrote:
> I'm still having trouble with calling a text file and I know its so 
> simple here is the code
> ----------------------------------------------------
> work = open('C:\Documents and Settings\Administrator\My 
> Documents\Chris\Python\Python\work.txt', 'r')
> for line in work.txt:
>     print line
> -----------------------------------------------------
> Ok so I know that the directory is right cause I'm not a complete 
> idiot but I keep getting an error? What am I missing here?
> -Chris-
> ------------------------------------------------------------------------
>
> --
> http://mail.python.org/mailman/listinfo/python-list
First of all, some terminology: You are not *calling* a file, you are 
*opening* it or reading.

Once you *open* the file, you store the resulting file object in a 
variable named "work", then to use that object to request a line, thus:

for line in work:
  print line


Hint: When you say "I keep getting an error",  you really have not 
provided mush useful information.   In the future, when you get an 
error, and you want help, spend the time to post the error message and 
traceback along with your question.   In this case, the problem was 
trivially easy to spot, but you'll get past these trivial problems 
soon.  Then you'll be more likely to find volunteers who will spend time 
to help *if* you spend the time to provide useful information.

Gary Herron




More information about the Python-list mailing list