2

I am just a beginner in Python. I created a file called as cc.py and saved in the following path :

C:/Python33/cc.py.

I am trying to run this file but nothing is happening.

In the python shell I am typing Python cc.py but I am getting the following error:

SyntaxError: invalid syntax

I tried an alternative :

>>> execfile('cc.py');
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    execfile('cc.py');
NameError: name 'execfile' is not defined

The file contains the following lines of code :

 import urllib

 htmlfile = urllib.urlopen("http://google.com")

 htmltext = htmlfile.read()

 print htmltext

How should I run this file? I am totally confused. Can someone help me out?

mcAfee
  • 209
  • 1
  • 3
  • 8
  • How were you "trying to run this file" and given the Syntax error I think we need to see the code, Have you got a simple print("Hello") working? – mmmmmm Nov 11 '13 at 14:22
  • The syntax error is coming because he is typing it in the interpreter, not command prompt. – aIKid Nov 11 '13 at 14:25

4 Answers4

1

In python 3, execfile no longer exists. You can open it and execute it manually:

def xfile(afile, globalz=None, localz=None):
    with open(afile, "r") as fh:
        exec(fh.read(), globalz, localz)

And to execute:

>>> xfile(r'C:\path\to\file\script.py')

Credit to: What is an alternative to execfile in Python 3?

That's how you execute files from the interpreter.

Another way, you can execute it from the command prompt. Just open it and type:

$ cd filepath
$ python file.py

About the script you're running, there's also a confusion. Whatever example you are following, it's a Python 2 example, yet you are using Python 3. Change the request line to this:

htmlfile = urllib.request.urlopen("http://google.com")

Hope this helps!

Community
  • 1
  • 1
aIKid
  • 26,968
  • 4
  • 39
  • 65
  • I tried this too .. I typed after opening cmd.exe : cd Python33 and then I typed `python cc.py` but nothing happened – mcAfee Nov 11 '13 at 14:26
  • where do I need to write `xfile` ? – mcAfee Nov 11 '13 at 14:27
  • Just copy paste the code to your interpreter, in the space `>>> `. – aIKid Nov 11 '13 at 14:28
  • I am getting the following error : `Traceback (most recent call last): File "", line 1, in xfile(r'C:\Python33\cc.py') File "", line 3, in xfile exec(fh.read(), globalz, localz) File "", line 3, in AttributeError: 'module' object has no attribute 'urlopen'` – mcAfee Nov 11 '13 at 14:31
  • should I uninstall Python3.3 and install Python2.7 instead ?? – mcAfee Nov 11 '13 at 15:08
  • That's up to you. If you feel like it, go ahead. But my solution is enough to fix your problem. – aIKid Nov 11 '13 at 15:09
1

print htmltext should be print(htmltext). Also, execfile() was removed from Python 3. It seems you are using a Python 2 book but running Python 3. These different versions of Python are incompatible, stick to one. For choosing which version, see this question.

An implemention of execfile():

def execfile(filename, *args, **kwargs):
    with open(filename) as fp:
        exec(fp.read(), *args, **kwargs)
Community
  • 1
  • 1
Ramchandra Apte
  • 4,033
  • 2
  • 24
  • 44
1

You wrote:

In the python shell I am typing Python cc.py but I am getting the following error:

SyntaxError: invalid syntax

If you want to run the python script, don't do it from the python shell. The "python" (not "Python") command needs to be run from a command prompt (DOS shell, terminal window, etc).

From a command prompt you should issue the command:

$ python cc.py

For a more complete description of the problem and solution, see Executing Scripts in the windows section of the python user guide, and also How do I run a python program under windows in the frequently asked question section of the python users guide.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0
import urllib.request
with urllib.request.urlopen("http://www.yourwebsiteurl.com") as url:
     htmltext = url.read()
     print (htmltext)