Created
April 9, 2018 08:49
-
-
Save cessor/616ac685373f20df1c5a299c228d29be to your computer and use it in GitHub Desktop.
Flatten a Directory by moving all files up one level.
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 shutil | |
| ROOT = '.' | |
| for dirpath, dirnames, filenames in os.walk(ROOT): | |
| if dirpath == ROOT: | |
| continue | |
| for file in filenames: | |
| source = os.path.join(dirpath, file) | |
| destination = os.path.join(ROOT, file) | |
| # Skip existing, | |
| # First file wins | |
| if os.path.exists(destination): | |
| os.remove(source) | |
| continue | |
| # Move file up one level | |
| shutil.move(source, ROOT) | |
| # Delete Empty Folder | |
| if not os.listdir(dirpath): | |
| os.rmdir(dirpath) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment