user5006486
user5006486

Reputation: 13

errors after upgrading to python3.4 on mac

After completing upgrading Python 2.7 to 3.4 on mac (10.10.3), I can't compile my codes.

    Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03) 
    [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
    Type "copyright", "credits" or "license()" for more information.
    >>> ================================ RESTART ================================
    >>> 
    Traceback (most recent call last):
      File "/Users/tpmac/preBS.py", line 31, in <module>
        doc = file(os.path.join(subdir,f)).read()
    NameError: name 'file' is not defined
    >>> 

these codes are working with python 2.7 on my system.

Upvotes: 1

Views: 37

Answers (1)

Padraic Cunningham
Padraic Cunningham

Reputation: 180512

There is no builtin file in python3, it has been removed just use open:

open(os.path.join(subdir,f)).read()

It would also be better use with when opening a file:

 with open(os.path.join(subdir,f)) as fle:
       doc = fle.read()

There is a comprehensive guide here on porting code from python2 to 3

Upvotes: 1

Related Questions