Skip to content

Instantly share code, notes, and snippets.

@gabyfle
Last active April 16, 2021 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabyfle/766b011978eb6f580fdb003d47795cc9 to your computer and use it in GitHub Desktop.
Save gabyfle/766b011978eb6f580fdb003d47795cc9 to your computer and use it in GitHub Desktop.
bz2 automated file compression
import os
import bz2
from pathlib import Path
files = list(Path('.').rglob('*.*'))
for file in files:
if file.name == __file__: continue
if file.suffix == '.bz2': continue
name = Path(str(file) + '.bz2')
compressed = bz2.compress(file.read_bytes())
if not name.exists(): name.touch()
name.write_bytes(compressed)
#/usr/bin/python
# Namecheap Python version: 2.6.6 (WTF IT SUCKS)
import os
import bz2
import glob
ignore = [
__file__,
'robot.txt',
'.htaccess',
'.ftpquota'
]
for root, subdirs, files in os.walk('.'): # change os.walk to the directory you need to compress (the behaviour of os.walk directories in python 2.6.6 is still a mystery for me
for file in files:
if file in ignore: continue
path = os.path.join(root, file)
if os.path.splitext(file)[1] == '.bz2': continue
with open(path, 'rb+') as f: # compress the binary data and write to a new file
new = path + '.bz2'
compress = bz2.compress(f.read())
if not os.path.exists(new): # create a new file name.ext.bz2
with open(new, 'a'):
os.utime(new, None)
with open(new, 'wb') as n:
n.write(compress)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment