0

I'm implementing a fast copy function for Python (because ain't nobody got time for shutil) however I'm getting a syntax error E901 on this line except (IOError, os.error), why: Here's the full code:

class CTError(Exception):

    def __init__(self, errors):
        self.errors = errors
try:
    O_BINARY = os.O_BINARY
except:
    O_BINARY = 0
READ_FLAGS = os.O_RDONLY | O_BINARY
WRITE_FLAGS = os.O_WRONLY | os.O_CREAT | os.O_TRUNC | O_BINARY
BUFFER_SIZE = 128 * 1024


def copyfile(src, dst):
    try:
        fin = os.open(src, READ_FLAGS)
        stat = os.fstat(fin)
        fout = os.open(dst, WRITE_FLAGS, stat.st_mode)
        for x in iter(lambda: os.read(fin, BUFFER_SIZE), ""):
            os.write(fout, x)
    finally:
        try:
            os.close(fin)
        except:
            pass
        try:
            os.close(fout)
        except:
            pass


def copytree(src, dst, symlinks=False, ignore=[]):
    names = os.listdir(src)

    if not os.path.exists(dst):
        os.makedirs(dst)
    errors = []
    for name in names:
        if name in ignore:
            continue
        srcname = os.path.join(src, name)
        dstname = os.path.join(dst, name)
        try:
            if symlinks and os.path.islink(srcname):
                linkto = os.readlink(srcname)
                os.symlink(linkto, dstname)
            elif os.path.isdir(srcname):
                copytree(srcname, dstname, symlinks, ignore)
            else:
                copyfile(srcname, dstname)
            # XXX What about devices, sockets etc.?
        except (IOError, os.error), why: #XXX Here's the bug!
            errors.append((srcname, dstname, str(why)))
        except CTError, err:
            errors.extend(err.errors)
    if errors:
        raise CTError(errors)

Why is this syntax invalid?

4
  • As an aside, this looks like it basically works like the functions in shutil. See shutil.copyfileobj(). Commented Aug 18, 2015 at 22:52
  • Are you using Python 3? Commented Aug 18, 2015 at 22:54
  • There's some minor changes that, reading from the comments on the code,I wrote this a while ago, made it a little faster. Do you know any better method for fast directory and file transfer? Commented Aug 18, 2015 at 22:55
  • @Thanatos yes, 3.4 I believe Commented Aug 18, 2015 at 22:56

1 Answer 1

1

In Python 2, this fragment's syntax appears valid (CPython 2.7.10 accepts it on my machine).

In Python 3, this syntax isn't valid. The syntax,

except <TYPEEXPR>, <VAR>:

was deprecated. It was replaced with,

except <TYPEEXPR> as <VAR>:

in Python 3. E.g.,

except (IOError, os.error) as why:

This syntax is valid in Python 2 (it was added in 2.6, I believe), and I find it easier to read, so I'd recommend using it in Python 2 over the deprecated syntax as well, especially since it's forward-compatible and Python ≤2.5 usage is pretty small nowadays, and probably not worth supporting; I believe most libraries have dropped support.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.