Last active
February 2, 2019 07:13
-
-
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
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 | |
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