Skip to content

Instantly share code, notes, and snippets.

@deltafactory
Last active August 29, 2015 14:23
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 deltafactory/6e6e93d7e3c3d23698cd to your computer and use it in GitHub Desktop.
Save deltafactory/6e6e93d7e3c3d23698cd to your computer and use it in GitHub Desktop.
Use python to list directory as tab-separated values
"""
List directory as tab-separated values
* Written, annotated, and hastily bug-checked by Jeff Brand (@deltafactory)
* Inspired by Rudy Trujillo, https://plus.google.com/u/0/+RudyTrujillo/posts/7yWsPXVvKmt
"""
# Import os and sys packages since we'll be using them. (Look for sys.* and os.* below.)
# More info: https://docs.python.org/3/reference/import.html
import os, sys
# Check for command-line arguments array to see if a path was specified, e.g: dir.py /home/deltafactory
# The first arg (index 0) is the script name. Index 1 is the only one we care about, if present.
if len( sys.argv ) > 1:
path = sys.argv[1]
# Otherwise, we want the current working directory.
else:
path = '.'
# Print tab-separated header. Note sep="\t" puts tabs between the elements.
# More info: https://docs.python.org/3/library/functions.html#print
print( 'Filename', 'Type', 'Size', 'Modified Time', sep="\t" )
# os.listdir() provides a list (array) of filenames from the specified path.
# The for-loop stores each filename into f to be used in the block within.
for f in os.listdir(path):
# Combine the starting path and filename into a "full" path to the file.
# os.path.join() takes care of OS-specifics
# More info: https://docs.python.org/2/library/os.path.html#os.path.join
filepath = os.path.join( path, f )
# Ternary operator used to set value for "Type" column: dir or file
fod = 'dir' if os.path.isdir( filepath ) else 'file'
# There is probably a better way to detect whether we can stat() a file without exception handling.
# Exception handling is a big topic..
# More info: https://docs.python.org/3.4/tutorial/errors.html
try:
# This throws an error with insufficient permission.
# When it works, it gets information about the file/dir including size and date.
# More info: https://docs.python.org/3/library/os.html#os.stat
fs = os.stat(filepath)
fsinfo = [fs.st_size, fs.st_mtime]
except:
# If an exception occurred in the "try" block above, enter some blanks.
# Otherwise, the previous file's data would remain and be printed below.
fsinfo = ['', '']
# Print columns: Filename, Type, Size, Modified Time - using tab character as separator
print( f, fod, fsinfo[0], fsinfo[1], sep="\t")
# Fin.
@deltafactory
Copy link
Author

OK, technically tab separated values, TSV.

@Goddur
Copy link

Goddur commented Jun 26, 2015

Is this intended to work as cut and paste?
Traceback (most recent call last):
File "c:dir.py", line 3, in
path = sys.argv[1] if len( sys.argv ) else '.'
IndexError: list index out of range
Press any key to continue . . .

@deltafactory
Copy link
Author

It was, though I had an error in my length check. I've replaced the ternary with an if/then (in case Python <2.5 is still relevant) and check for the arg length > 1 as originally intended.

@Goddur
Copy link

Goddur commented Jun 26, 2015

Great! It ran first time now to go and dissect what it is doing. Thank you so very much.

@Goddur
Copy link

Goddur commented Jun 26, 2015

Wanted to take a minute to thank you for the detailed comments you added to your code it was clear and concise. You went beyond what most would have done. Again thank you for your help understanding some of the rudimentary aspects of coding in python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment