Created
July 31, 2019 14:36
-
-
Save jasiek/2569a167e1459f64f5036a04e35bed0f to your computer and use it in GitHub Desktop.
This file contains 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 | |
import sys | |
# | |
# For each file in a given directory, create a directory with the first letter of the file, and then move the file | |
# into that directory. | |
# | |
def main(): | |
directory = sys.argv[1] | |
os.chdir(directory) | |
files = os.listdir('.') | |
letters = {} | |
for f in files: | |
if f.startswith('.'): | |
continue | |
if os.path.isfile(f): | |
ltr = f[0].lower() | |
if not ltr in letters.keys(): | |
letters[ltr] = [] | |
letters[ltr].append(f) | |
for ltr in letters.keys(): | |
os.mkdir(ltr) | |
for f in letters[ltr]: | |
shutil.move(f, os.path.join(ltr, f)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment