How can I make this program smaller?

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.

ballaw

Posted 2012-09-02T20:48:37.833

Reputation: 11

Question was closed 2012-09-06T13:01:37.467

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.283

p=os.path is 3 characters shorter than os.path as p. – James – 2012-09-03T09:29:45.553

1

Probably belongs on http://codereview.stackexchange.com ?

– Paul R – 2012-09-03T12:05:02.910

I 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

Answers

1

There is a bug with your minified version: s[:-0] gives you the empty string, not the complete string as you anticipate with s[:-i==len(a)-1]. To rectify this, you will need to either ignore preceding bars or calculate the a little more thoroughly. The shorter version would be to ignore preceding bars and not worry about passing spacing to further recursions. Since it permits the removal of several variables, I worked your code to do this

Your tree function is only called once, I removed it.

s=s+'|' and i=i+1 can be collapsed into s+='|' and i+=1

os.path as p can become p=os.path (Thanks James)

p.join(c,f) can become c+'/'+f (Thanks beary605)

New version:

import os,p=os.path
def w(c,d):
    for f in os.listdir(c):
        print '  '*(d+1)+'|--'+f;j=c+'/'+f
        if p.isdir(j): w(j,d+1)
z=p.curdir;print z;w(p.curdir,0)

If you really wanted to go crazy, you can make put this all on one line and get it down to 143 bytes (on linux)

import os,p=os.path
def w(c,d):
 for f in os.listdir(c):print'  '*(d+1)+'|--'+f;j=c+'/'+f;w(j,d+1)if p.isdir(j)else 0
z=p.curdir;print z;w(z,0)

Danny Kirchmeier

Posted 2012-09-02T20:48:37.833

Reputation: 141

Remove unecessary whitespace (5chars), and also replace p.join(c,f) with c+'/'+f (4chars): for f in os.listdir(c):print' '*(d+1)+'|--'+f;j=c+'/'+f;w(j,d+1)if p.isdir(j)else 0 – beary605 – 2012-09-04T03:46:49.013

@beary605 Will c+'/'+f yield the same result as p.join(c,f) on a windows box? – Danny Kirchmeier – 2012-09-04T10:13:03.240

It will, does it not on Linux? – beary605 – 2012-09-04T14:55:33.947

1

The docs say os.path.join uses os.sep which "is '/' for POSIX and '\\' for Windows".

– Danny Kirchmeier – 2012-09-04T15:42:02.270

3It works in Windows. Windows is pretty lenient with their file paths. – beary605 – 2012-09-05T03:37:33.637