Return to Snippet

Revision: 26641
at May 6, 2010 10:23 by throb


Updated Code
####################################################
##
##  Prints a dir structure to a text file
##
##  Usage : python printdir.py [path ala z:/mypath or /mypath/foo
##
####################################################

import os, sys

dir = sys.argv[1]
outName = 'dirpaths.txt'
f = open(outName,'w')

# write initial dir for reference
f.write (dir + '\n')

count = 0

for item in os.walk(dir):
    count = count + 1
    item = item[0].replace('\\','/')
    curPath = item.split('/')
    if count == 1:
        curPath = item
    else:
        curPath = '\t' * (len(curPath)-1) + curPath[len(curPath)-1]
    
    f.write(curPath + '\n')
    print curPath
  

f.close
if os.path.exists(outName) :
    print 'File saved to: %s' % (outName)

Revision: 26640
at May 5, 2010 07:55 by throb


Initial Code
####################################################
##
##  Prints a dir structure to a text file
##
##  Usage : python printdir.py [path ala z:/mypath or /mypath/foo
##
####################################################

import os, sys

dir = sys.argv[1]
outName = 'dirpaths.txt'
f = open(outName,'w')

for item in os.walk(dir):
    item = item[0].replace('\\','/')
    f.write(item + '\n')

f.close
if os.path.exists(outName) :
    print 'File saved to: %s' % (outName)

Initial URL

                                

Initial Description

                                

Initial Title
List all directories in a path and save the information to a text file

Initial Tags
file, python, directory

Initial Language
Python