1
I am trying my hand at writing super small programs. I wrote a simple "tree" display program for showing a graphical representation of a folder hierarchy. I have made it small in all the ways I can think of. I was just wondering if you guys had more creative ways to "minify" it further. Here is the small version as well as a human-readable version.
import os,os.path as p
def w(c,d,s):
a=os.listdir(c);s=s+'|';i=0
for f in a:
print s+'--'+f;j=p.join(c,f);
if p.isdir(j):w(j,d+1,s[:-i==len(a)-1]+' '*(d+1));i=i+1
def t(c):print c;w(c,0,'')
t(p.curdir)
That's a total of 236 bytes
import os
def treeWrap(curdir,depth,pipeStr):
files = os.listdir(curdir)
pipeStr = pipeStr + '|'
for f in files:
print pipeStr + '--' + f
if os.path.isdir(os.path.join(curdir,f)):
dir = os.path.join(curdir,f)
if files.index(f) == (len(files)-1):
treeWrap(dir,depth+1,pipeStr[:-1] + ' '*(depth+1))
else:
treeWrap(dir,depth+1,pipeStr + ' '*(depth+1))
def tree(curdir):
print curdir
treeWrap(curdir,0,'')
tree(os.path.curdir)
Oh, in addition, does anyone know how I can use the unicode ├,└,─ in this program? Whenever I use them they appear as garbage in the console and the idle interpreter.
1
A bunch of generic minifying python techniques are here: http://codegolf.stackexchange.com/questions/54/tips-for-golfing-in-python
– Keith Randall – 2012-09-02T23:55:06.283p=os.path
is 3 characters shorter thanos.path as p
. – James – 2012-09-03T09:29:45.5531
Probably belongs on http://codereview.stackexchange.com ?
– Paul R – 2012-09-03T12:05:02.910I believe you got advised to try this here on Stack Overflow, but the person who suggested that wasn't entirely familiar with our FAQ. Each "question" here is expected to pose a game or challenge with a well defined winning criteria and enough flexibility to be fun for participants. This is not a site for general question (with the special exception of the "hints" questions). – dmckee --- ex-moderator kitten – 2012-09-06T13:04:31.253