0

I've got two tasks:

  1. I've set up my digital library in the format of a Dewey Decimal Classification, so I've got a 3-deep hierarchy of 10 + 100 + 1000 folders, with directories sometimes going a little deeper. This library structure contains my "books" that I would like to list in a catalog (perhaps a searchable text document). It would be preferable, though not absolutely necessary, if I could view the parent directory name in a separate column next to each "book".

  2. The problem is that some of the "books" in my library are folders that stand alone as items. I planned ahead when I devised this system and made it so that each item in my library would contain a tag in []s that would contain the author name, for instance, and so the idea is that I would try to perform a recursive listing of all of this, but end each recursion when it encounters anything with a [ in the name, directory or file.

How might I go about this? I know a bit of Python (which is originally what I used to create the library structure), and since this is on an external hard drive, I can do this in either Windows or Linux. My rough idea was to perform some sort of a recursive listing that would check the name of each directory or file for a [, and if it did, stop and add it (along with the name of the parent directory) to a list. I don't have any idea where to start.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
bittenfig
  • 5
  • 1
  • 1
    A Google search of "traverse directory tree in python" immediately yielded this, and a few other good links: http://www.pythoncentral.io/how-to-traverse-a-directory-tree-in-python-guide-to-os-walk/. You can add the string matching as needed. – lurker Feb 18 '14 at 00:19
  • Build a class! `class Library` can have an `update` method where it does all this walking, and you can add features like your search while you do it! `class Book` could contain the Dewey Decimal classification of each book, so you could do something like `[Book(file) for root,dir,file in os.walk(rootDir)]`. Do something cool with this!! – Adam Smith Feb 18 '14 at 00:41
  • That said, I don't really understand how your library is laid out with your "books that are directories" concern. Can you elaborate? – Adam Smith Feb 18 '14 at 00:43

1 Answers1

2

The answer is based on this where

  • dirName: The next directory it found.
  • subdirList: A list of sub-directories in the current directory.
  • fileList: A list of files in the current directory.

Deletion cannot be done by list comprehension, because we have to "modify the subdirList in-place". Instead, we delete with enumerate on a deep copy of the list so that the counter i wouldn't be skipped after deletions while the original list gets modified.

I haven't tried it so don't trust this 100%.

# Import the os module, for the os.walk function
import os

# Set the directory you want to start from
rootDir = '.'
for dirName, subdirList, fileList in os.walk(rootDir):
    print('Found directory: %s' % dirName)
    for fname in fileList:
        print('\t%s' % fname)

    for i, elem in reversed(list(enumerate(subdirList[:]))):
        if "[" in elem:
            del subdirList[i]
Community
  • 1
  • 1
randwa1k
  • 1,502
  • 4
  • 19
  • 30
  • Your last loop won't delete multiple items correctly, since the indexes of later items in the original `subdirList` will be changed when you delete an earlier one. You either need to iterate backwards (from the end to the beginning) or use a different approach to modify the list in place (such as a slice assignment: `subdirList[:] = [elem for elem in subdirList if "[" not in elem]`). – Blckknght Feb 18 '14 at 00:52
  • Good point. We can't use list comprehension though because it's not in place. I'm editing it. – randwa1k Feb 18 '14 at 00:58
  • It's fixed to enumerate backwards now. – randwa1k Feb 18 '14 at 01:04
  • After a little bit of tinkering, so far, this appears to work as expected, if not perfectly. I've edited this answer to reflect my modification. This shows the directories as "books" or items, just like the other files. Thanks for the help! – bittenfig Feb 18 '14 at 04:53