Skip to content

Instantly share code, notes, and snippets.

@humpydonkey
Last active February 2, 2019 07:13
Show Gist options
  • Save humpydonkey/e662810f4957ffb3563dffe6249173a2 to your computer and use it in GitHub Desktop.
Save humpydonkey/e662810f4957ffb3563dffe6249173a2 to your computer and use it in GitHub Desktop.
[flat directory]Move all the files under src directory to the root of src directory. #os #python #file #tool
import os
def flat_directory(source_dir: str) -> None:
"""
Move all the files under src directory to the root of src directory.
Flatten the structure.
"""
count = 0
for currDir, subDirs, files in os.walk(source_dir):
if currDir == source_dir:
continue
for f in files:
move_from_path = os.path.join(currDir, f)
move_to_path = os.path.join(source_dir, f)
os.rename(move_from_path, move_to_path)
count += 1
print(f"{count} files have been move to {source_dir}")
def remove_empty_dir(source_dir: str) -> None:
count = 0
for currDir, subDirs, files in os.walk(source_dir):
if not files and not subDirs:
os.rmdir(currDir)
count += 1
else:
print(currDir)
print(f"Removed {count} empty directories.")
if __name__ == '__main__':
print("Start flattening files.")
source_dir = '/Volumes/hello/world/iphone/path'
flat_directory(source_dir)
remove_empty_dir(source_dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment