Frage

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.

War es hilfreich?

Lösung

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]
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top