Why does my Python script fail with syntax errors?

Naive

While running the simple Python program below, I'm getting the following error:

./url_test.py: line 2: syntax error near unexpected token `('                  
./url_test.py: line 2: `response = urllib2.urlopen('http://python.org/')'

import urllib2      
response = urllib2.urlopen('http://python.org/')  
print "Response:", response

# Get the URL. This gets the real URL. 
print "The URL is: ", response.geturl()

# Getting the code
print "This gets the code: ", response.code

# Get the Headers. 
# This returns a dictionary-like object that describes the page fetched, 
# particularly the headers sent by the server
print "The Headers are: ", response.info()

# Get the date part of the header
print "The Date is: ", response.info()['date']

# Get the server part of the header
print "The Server is: ", response.info()['server']

# Get all data
html = response.read()
print "Get all data: ", html

# Get only the length
print "Get the length :", len(html)

# Showing that the file object is iterable
for line in response:
 print line.rstrip()

# Note that the rstrip strips the trailing newlines and carriage returns before
# printing the output.
Radu Rădeanu

You missed to add a shebang at the beginning of the script to tell the shell to interpret your script as a python script. Something like this:

#!/usr/bin/python

or:

#!/usr/bin/env python

See also: Why do people write #!/usr/bin/env python on the first line of a Python script?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does my Python script fail with syntax errors?

From Java

Why don't I get any syntax errors when I execute my Python script with Perl?

From Dev

why does this script fail?

From Dev

Why does cron silently fail to run sudo stuff in my script?

From Dev

Why does my LDAP redirect script fail with one user?

From Dev

Why does cron silently fail to run sudo stuff in my script?

From Dev

Why does my script fail when running from udev?

From Dev

Why does my NSMutableURLRequest fail?

From Dev

Python: Why does my 'less than' test fail?

From Dev

Why does this sign in form fail with no errors?

From Dev

Why does my Python script not work at all?

From Dev

Why does my python script delete itself?

From Dev

Why does this ftp get command work in my shell script but fail in cron

From Dev

Why does a second parameter cause this script to fail?

From Dev

An Python syntax error in my script

From Java

Why does assembly binding fail in my project?

From Dev

Why does my cross-compiling fail?

From Dev

Why does my redirected CORS request fail?

From Dev

Why does my rspec test fail?

From Dev

Why does my kernel fail to build?

From Dev

Why does my linq to sql query fail?

From Dev

Why does my pointer arithmetic fail in this array

From Dev

Why does my wifi fail to stay connected?

From Dev

Why does my jasmine tests fail on this directive?

From Dev

Why does my Vapor query always fail?

From Dev

Why Does My FAMILY Query Fail?

From Dev

Why does (python) gspread work fine from my Mac but fail silently from my CentOS host

From Dev

Why does (python) gspread work fine from my Mac but fail silently from my CentOS host

From Dev

Why does PayPal's Express Checkout fail with no errors?

Related Related

HotTag

Archive