Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import shutil
- def sum_of_dir(path):
- for root, dirs, files in os.walk(path):
- size = 0
- for file in files:
- with open(os.path.join(root, file), 'r') as f:
- size += int(f.readline())
- for dir in dirs:
- size += sum_of_dir(os.path.join(root, dir))
- return size
- with open('input1.txt', 'r') as f:
- rows = list(map(lambda x: x.strip(), f.readlines()))
- my_cwd = os.getcwd()
- home = os.path.join(my_cwd, 'tmp')
- if os.path.exists(home):
- shutil.rmtree(home)
- os.mkdir(home)
- current_position = home
- for row in rows:
- if row.startswith('$'): # command
- command = row.strip('$ ')
- parts = command.split(' ')
- if parts[0] == 'cd':
- target = parts[1]
- if target == '..':
- folders = current_position.split('\\')
- current_position = '\\'.join(folders[:-1])
- else:
- if target == '/':
- target = home
- current_position = os.path.join(current_position, target)
- elif parts[0]:
- continue
- else:
- if row.startswith('dir'):
- name = row.split()[1]
- os.mkdir(os.path.join(current_position, name))
- else:
- size, file_name = row.split()
- with open(os.path.join(current_position, file_name), 'w') as f:
- f.write(size)
- directory_size = {}
- for root, dirs, files in os.walk(home):
- this = root.split('\\')[-1]
- size = 0
- for file in files:
- with open(os.path.join(root, file), 'r') as f:
- size += int(f.readline())
- for dir in dirs:
- size += sum_of_dir(os.path.join(root, dir))
- directory_size[this] = size
- res = 0
- for key, val in directory_size.items():
- if val <= 100000:
- res += val
- print(res)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement