Last active
April 16, 2021 19:18
-
-
Save gabyfle/766b011978eb6f580fdb003d47795cc9 to your computer and use it in GitHub Desktop.
bz2 automated file compression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/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