0

I have a file containing a file "dir.txt" with the below data:

/home/abc/a.txt
/home/abc/b.txt
/home/xyz/test
/home/xyz/test/d.txt
/home/xyz/test/e.txt
/home/xyz/test/f.txt
/home/xyz
/home/xyz/g.txt

I want to parse the file and get the output like

/home/abc/a.txt
          b.txt
/home/xyz/test/d.txt
               e.txt
               f.txt
/home/xyz/g.txt

Using python, basically need to print the content in a tree format. How would I process it?

3
  • your edit completely changes the original question! If you need to expand the already-posted solutions you need to narrow down your problem and post another question! Commented Feb 18, 2010 at 13:18
  • wtf? Panther24, we send you codez of a silver plate and you're coming in and makes all of look like idiots who don't know/understand basics of Python! Commented Feb 18, 2010 at 13:29
  • @SilentGhost, sorry, but am just a new kid on the block learning new stuff. Don't bother do look at what I've posted, I should be able to handle it. Cheers!! Commented Feb 18, 2010 at 17:46

5 Answers 5

4

you need to use os.path.split on every path, find the first dirname and print path as it is. find it length and print so many spaces before next basename, on change of the dirname repeat as before.

>>> import os.path    
>>> olddir = None
>>> for name in open('input.txt'):
    dirname, fname = os.path.split(name)
    if olddir != dirname:
        prefix = ' ' * (len(dirname) +1)
        olddir = dirname
        print(name)
    else:
        print(prefix + fname)


/home/abc/a.txt
          b.txt
/home/xyz/test/d.txt
               e.txt
               f.txt
/home/xyz/g.txt
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

import os.path

txt = """/home/abc/a.txt
/home/abc/b.txt
/home/xyz/test/d.txt
/home/xyz/test/e.txt
/home/xyz/test/f.txt
/home/xyz/g.txt"""

last_d = ''
for l in txt.split('\n'):
    (d, n) = os.path.split(l)
    if d == last_d:
        d = ' ' * len(last_d)
    else:
        last_d = d
    print('%s/%s' % (d, n))

2 Comments

@ghostdog74: there was no indication in the answer that it might be the case, even if it is so, adding sorted to for clause is trivial.
I've changed the source a little bit!!
2

@Op,use a dictionary. Use the paths as the key and the file names as values

from collections import defaultdict
d=defaultdict(list)
for line in open("file"):
    line=line.strip()
    s='/'.join(line.split("/")[:-1])
    d[s].append(line.split("/")[-1])

for i,j in d.iteritems():
    print i,j

output

$ ./python.py
/home/xyz ['g.txt']
/home/xyz/test ['d.txt', 'e.txt', 'f.txt']
/home/abc ['a.txt', 'b.txt']

Do the formatting as described by the answers others had posted.

Comments

0
>>> filenames="""/home/abc/a.txt
... /home/abc/b.txt
... /home/xyz/test/d.txt
... /home/xyz/test/e.txt
... /home/xyz/test/f.txt
... /home/xyz/g.txt""".split()
>>> 
>>> import os
>>> prev=''
>>> for n in filenames:
...     path,name = os.path.split(n)    
...     if path==prev:
...         print " "*len(prev)+" "+name
...     else:
...         print n
...         prev=path
... 
/home/abc/a.txt
          b.txt
/home/xyz/test/d.txt
               e.txt
               f.txt
/home/xyz/g.txt

Comments

0

This is an alternate take that offers a different output, just in case the OP would prefer this format:

/home/abc/a.txt
          b.txt
      xyz/test/d.txt
               e.txt
               f.txt
          g.txt

then this code:

import os

def pretty_printer(seq_of_strings):
    previous_line= ''
    for line in seq_of_strings:
        last_sep= os.path.commonprefix([previous_line, line]).rfind(os.path.sep)+1
        yield ' '*last_sep + line[last_sep:]
        previous_line= line

might do the trick.

Should the OP comment that they don't need it at all, I'll delete this answer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.