พิมพ์ Directory

  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.
  • warning: realpath() [function.realpath]: SAFE MODE Restriction in effect. The script whose uid is 1005 is not allowed to access /tmp owned by uid 0 in /var/www/sites/sugree/codenone.com/subdomains/www/html/includes/file.inc on line 190.

อืม ผมเขียนไม่ค่อยสวยเอาซะเลย

#!/usr/bin/env python
 
import sys
import os
 
def tlist(root,indent=''):
    if indent:
        print '%s%s' % (indent,os.path.basename(root))
    else:
        print '%s%s' % (indent,os.path.abspath(root))
    entries = os.listdir(root)
    indents = ['|-- ']*len(entries)
    if indents:
        indents[-1] = '`-- '
    indent = indent.replace('-',' ').replace('`',' ')
    for ind,ent in zip(indents,entries):
        path = os.path.join(root,ent)
        if not os.path.isdir(path):
            print '%s%s%s' % (indent,ind,ent)
        else:
            tlist(path,'%s%s' % (indent,ind))
 
if __name__ == '__main__':
    rootdir = os.getcwd()
    if len(sys.argv) > 1:
        rootdir = sys.argv[1]
 
    tlist(rootdir)

รู้สึกทะแม่งๆ ไม่รู้ว่าแบบไหนดีกว่ากัน

def tlist(root,indent=''):
    if indent:
        print '%s%s' % (indent,os.path.basename(root))
    else:
        print '%s%s' % (indent,os.path.abspath(root))
    entries = os.listdir(root)
    indent = indent.replace('-',' ').replace('`',' ')
    for i,ent in enumerate(entries):
        if i == len(entries)-1:
            ind = '`-- '
        else:
            ind = '|-- '
        path = os.path.join(root,ent)
        if not os.path.isdir(path):
            print '%s%s%s' % (indent,ind,ent)
        else:
            tlist(path,'%s%s' % (indent,ind))

หรือแบบนี้

def tlist(root,indent=''):
    if indent:
        print indent+os.path.basename(root)
    else:
        print indent+os.path.abspath(root)
    entries = os.listdir(root)
    indent = indent.replace('-',' ').replace('`',' ')
    for i,ent in enumerate(entries):
        if i == len(entries)-1:
            ind = indent+'`-- '
        else:
            ind = indent+'|-- '
        path = os.path.join(root,ent)
        if not os.path.isdir(path):
            print ind+ent
        else:
            tlist(path,ind)

ย้าย Codenone

ประกาศย้าย Codenone ไปใช้ Forum ของ Blognone แทนครับ ตามไปตั้งกระทู้ต่อได้ที่ Codenone Forum (รายละเอียดอ่านจากกระทู้ ย้าย Codenone ไปรวมกับ Blognone)

กระทู้เก่าๆ จะย้ายตามไปในภายหลัง ตอนนี้ปิดการโพสต์กระทู้ไว้ เหลือไว้เฉพาะอ้างอิงเท่านั้น