ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-Tutor
python-Tutor
Re: [Tutor] "=" invalid syntax ?
by Daniel Ehrenberg other posts by this author
Jan 31 2004 7:09PM messages near this date
[Tutor] "=" invalid syntax ? | Re: [Tutor] "=" invalid syntax ?
Xuer wrote:
>  print all lines of a simple file
>  
>  test.py
>  -----------------------------------------------
>  #!/usr/bin/python
>  
>  try:
>          fi = open('test.py', 'r')
>  except IOError:
>          print 'Can\'t open file for reading.'
>          sys.exit(0)
>  while (line=fi.readline()):
>          print line
>  -----------------------------------------------
>  then run it
>  
>  $./test.py
>    File "./test.py", line 8
>      while (line=fi.readline()):
>                 ^
>  SyntaxError: invalid syntax
>  
>  what's the problem?
>  
>  thanks and regards :)

In Python, = is a statement. You can still loop
through files, however. Here's how I would write the
program instead:

#!/usr/bin/python

try:
    fi = open('test.py', 'r')
    for line in fi:
        print line
except IOError:
    print "Can't open file for reading."
finally:
    fi.close()

Daniel Ehrenberg

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free web site building tool. Try it!
http://webhosting.yahoo.com/ps/sb/

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Xuer
Daniel Ehrenberg
Karl =?iso-8859-1?q?Pfl=E4sterer?=
Lloyd Kvam
Daniel Ehrenberg

Daniel Ehrenberg
Danny Yoo
Tim Johnson
Danny Yoo
Gregor Lingl

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved