Skip to content

Instantly share code, notes, and snippets.

@jasiek
Created July 31, 2019 14:36
Show Gist options
  • Save jasiek/2569a167e1459f64f5036a04e35bed0f to your computer and use it in GitHub Desktop.
Save jasiek/2569a167e1459f64f5036a04e35bed0f to your computer and use it in GitHub Desktop.
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