How to decompress a .bz2 file in Google Collab

1

The answers on this site mostly suggest using bzip2 and bunzip, but I can't install them in Google Collab.

user3656142

Posted 2019-08-01T23:25:47.063

Reputation: 13

Answers

0

You can run external commands (shell commands) by prepending them with a !, and bzip2 and bunzip2 are standard Linux commands and should be pre-installed and available to you.

[ ] !bunzip2 path/to/myfile.bz2

Or:

[ ] !bzip2 -d path/to/myfile.bz2

If that doesn't work, you can then use Python code to access the file, in which case the bz2 module will be helpful.

[ ] import bz2, shutil
    with bz2.BZ2File('path/to/compressed.bz2') as in:
        with open('path/to/uncompressed', 'w') as out:
            shutil.copyfileobj(in, out)

filbranden

Posted 2019-08-01T23:25:47.063

Reputation: 1 058