Created
January 12, 2019 06:49
-
-
Save avkosme/93099ac74068ac7a46f180c51fc3bcf1 to your computer and use it in GitHub Desktop.
Copy files split a folder by size
This file contains hidden or 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 subprocess | |
from shutil import copyfile | |
def main(): | |
src = '/tmp/src' | |
dest = '/tmp/dest' | |
size_dir = 190 | |
def du(path): | |
return subprocess.check_output(['du', '-sm', path]).split()[0].decode('utf-8') | |
def files(path): | |
for file in os.listdir(path): | |
if os.path.isfile(os.path.join(path, file)): | |
yield file | |
count = 1 | |
for file in files(src): | |
dir_path = '/'.join((dest, str(count))) | |
try: | |
os.mkdir(dir_path) | |
except FileExistsError: | |
pass | |
size = du(dir_path) | |
if int(size) > size_dir: | |
count += 1 | |
dir_path = '/'.join((dest, str(count))) | |
os.mkdir(dir_path) | |
copy_src = '/'.join((src, file)) | |
copy_dest = '/'.join((dir_path, file)) | |
copyfile(copy_src, copy_dest) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment