0

I am looking for some kind of software that will take a word list in some Unicode encoding and use the words in that word list to predictably generate path names for files and folders.

The important part is that the output should be predictable. So whether it's a seed value that I need to provide or whether the software does its magic on its own without such a seed value is unimportant.

The goal is to use the generated list of paths to generate said files and folders in order to showcase another software that works primarily with file and folder hierarchies, visualizing their contents.

NB: a FLOSS solution would be preferred. The operating system can be either any modern Windows or Linux, but scripted/portable solutions would be preferred.


If the word list would be

foo
bar
baz

the output might be something like

foo/bar/baz
foo/foo/bar/baz
foo/foo/foo/bar/baz
foo/foo/foo/bar/baz.foo
foo/foo/foo/bar/baz.bar
foo/foo/foo/bar/baz.baz
...

... up to some hardcoded or configurable maximum path length. The important point is that the output should be predictably the same for the same input. Not all combinations need to be generated.

  • 4
    Can you provide a brief sample of what the list would look like and what output you expect? – techturtle Jun 29 '18 at 20:49
  • What techturtle asked... I have a few hours to kill after homework is done, and I like problems like this. Also may steal it as an assignmnet for a programming class i'm teaching this fall... – ivanivan Jun 29 '18 at 22:52
  • Also, if you're editing your question for the example, pleas also include what OS it should be for. – Izzy Jun 30 '18 at 10:07
1

Daft as it sounds it is easier if all combinations are done, (for predictability).

I would of course do this in Python as it is not only easy to do this in but also meets the other criteria being: FOSS, Cross Platform, Scripted & Portable. Using Python 3 all strings are Unicode in python 2 you will need to preceed any Unicode strings with the letter u i.e. u"Üηιçοďε Filename" to mark them as Unicode.

Do note that some Unicode & Ascii characters & strings will not make valid path elements.

Also note that with the following implementation if you wish to have duplicate file you will need to have duplicates in the list.

import os
import itertools
wordlist = ["foo", "bar", "baz", "spam", "Üηιçοďε Filename"]
MAXPATH = 100
for pathcount in range(1, len(wordlist)):  # dirs will  be one or more wordlist elements
    for pathels in itertools.permutations(wordlist, pathcount):  # Every permutation of pathcount elements from wordlist
        dirname = os.path.join(*pathels)
        if MAXPATH > len(dirname):
            try:
                os.makedirs(dirname)  # Create the directory including any parents
            except OSError:
                print("Directory Name:", dirname, "could not be created")
                continue
            for fileels in itertools.permutations(wordlist, 2):  # Every permutation of
                filename = '.'.join(fileels)
                fullpath = os.path.join(dirname, filename)
                if MAXPATH > len(fullpath):
                    try:
                        with open(fullpath, 'wt') as newfile:  # Create a file
                            newfile.write("Something\n")  # With something in it
                    except OSError:
                        print("File:", fullpath, "Could not be created")

Running this on Windows 10 with just the 5 names gave a directory tree with 617 directories and 4100 files in next to no time. enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.