w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Run Python File In Python Shell or Directly

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	ofilescript.py')

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

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!





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.