Skip to content

Instantly share code, notes, and snippets.

@EronHennessey
Created January 26, 2016 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EronHennessey/e71b7ada49a4bed2bb94 to your computer and use it in GitHub Desktop.
Save EronHennessey/e71b7ada49a4bed2bb94 to your computer and use it in GitHub Desktop.
An investigation of file system entries in python.
#!/usr/bin/env python
import sys, os, shutil
# Check to see what entities are in the file system and categorize them by
# type.
def analyze_path(path):
"""Get the file system entries in a path and categorize them."""
categorized = {
'symlinks': [],
'dirs': [],
'files': [],
'other': [],
}
for entry in os.listdir(path):
fullpath = os.path.abspath(os.path.join(path, entry))
if os.path.islink(fullpath):
categorized['symlinks'].append(entry)
elif os.path.isdir(fullpath):
categorized['dirs'].append(entry)
elif os.path.isfile(fullpath):
categorized['files'].append(entry)
else: # this should never happen...?
categorized['other'].append(entry)
return categorized
if __name__ == '__main__':
pathname = None
if (len(sys.argv) < 2):
pathname = os.getcwd()
else:
pathname = os.path.abspath(sys.argv[1])
# beware of any funny business...
if not os.path.isdir(pathname):
print("It's a cryin' shame, but the path you entered is *not* a valid directory!")
sys.exit(1)
print("\n" + pathname)
categorized = analyze_path(pathname)
# list the files in each category, as long as the category has something in it.
for key in categorized.keys():
entries = categorized[key]
if len(entries) > 0:
print("\n%s:" % key.capitalize())
for e in entries:
print(" " + e)
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment