Skip to content

Instantly share code, notes, and snippets.

Last active June 30, 2019 15:09
What would you like to do?
Fragments files in the directory so they can be pushed into Github.
# Recursively goes through the TARGET directory and zips files that are
# over SIZE_LIMIT bytes into PART_SIZE_7z parts, deleting the originals.
# Uses 7zip utility that needs to be ADDED INTO THE $PATH.
# Results in a directory structure that can be pushed into
# the GitHub without issues with oversized files.
import os, sys, subprocess
from time import sleep
# Path to the folder that should be fragmented.
TARGET = "X:\\_"
# Maximum allowed filesize in bytes.
SIZE_LIMIT = 95000000
# 7-zip size of the part file.
PART_SIZE_7z = "95m"
def obtain_all_filepaths(target_dir):
files = []
for r, d, f in os.walk(target_dir):
for file in f:
files.append(os.path.join(r, file))
print("Discovered: %s files." % len(files))
return files
def get_files_over_limit(files):
large = []
for f in files:
size = os.stat(f).st_size
if size >= SIZE_LIMIT:
print("Discovered: %s files over the size limit." % len(large))
return large
def zip_file_into_parts(path):
print("Splitting: %s" % path)
command = ["7z", "a", "%s_part" % path, path, "-v%" % PART_SIZE_7z]
system = subprocess.Popen(command, shell=True)
returncode = system.wait()
sleep(3) # Just to be sure 7zip releases the file.
print("File split. Deleting the original.")
return True
files = obtain_all_filepaths(TARGET)
large = get_files_over_limit(files)
for file in large:
except PermissionError as e:
print("PermissionError: %s" % e)
print("Deleting failed for: %s" % file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment