recursive traversal of file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Xavier Decoret

    recursive traversal of file

    I am reading the lines of a file, executing appropriate command if a
    pattern is found. One of the pattern can be a input command whose effect
    should be to #include the file (possibly recursively)

    The main loop looks like this:

    data=[]

    try:
    file = open(fileName)
    line = file.readline()
    while line:
    if matchInputPatte rn(line,inputFi le):
    # help me here to parse inputFile
    elif matchDataPatter n(line):
    data.append(1)
    line = file.readline()
    except IOError, e:
    print 'I couldn\'t open file name',fileName
    sys.exit(1)

    Can you tell me if there is a simple way to do the part that says #help
    me!. Should I do a recursive function?

    --
    +-------------------------------------------------+
    | Xavier Décoret - Post Doct |
    | Graphics Lab (LCS) - MIT |
    | mailto: decoret@graphic s.lcs.mit.edu |
    | home : http://www.graphics.lc s.mit.edu/~decoret|
    +-------------------------------------------------+

  • Hans Nowak

    #2
    Re: recursive traversal of file

    Xavier Decoret wrote:[color=blue]
    > I am reading the lines of a file, executing appropriate command if a
    > pattern is found. One of the pattern can be a input command whose effect
    > should be to #include the file (possibly recursively)
    >
    > The main loop looks like this:
    >
    > data=[]
    >
    > try:
    > file = open(fileName)
    > line = file.readline()
    > while line:
    > if matchInputPatte rn(line,inputFi le):
    > # help me here to parse inputFile
    > elif matchDataPatter n(line):
    > data.append(1)
    > line = file.readline()
    > except IOError, e:
    > print 'I couldn\'t open file name',fileName
    > sys.exit(1)
    >
    > Can you tell me if there is a simple way to do the part that says #help
    > me!. Should I do a recursive function?[/color]

    You could wrap all this in a function, let's call it import_file or whatever.
    Then, upon encountering the #include pattern, you could do,

    if matchInputPatte rn(line, inputFile):
    import_file(som e_filename_extr acted_from_patt ern)

    This *should* work, although I didn't test it. Also, it assumes that recursive
    function calls share the same list ('data'). It would be cleaner to have
    import_file create, fill and return its own list. In pseudocode:

    def import_file(fil ename):
    data = []
    ...
    for line in lines:
    if (pattern matches):
    z = import_file(fil ename_from_patt ern)
    data.extend(z)
    ...

    return data

    HTH,

    --
    Hans (hans@zephyrfal con.org)




    Comment

    • Paul McGuire

      #3
      Re: recursive traversal of file

      To prevent a recursive infinite loop, keep a list of the current chain of
      filenames being imported. Before importing filename_from_p attern, make sure
      it is not in the list (else you will eventually loop through the same files
      until your stack blows up or similar dire consequence). If the file is in
      the list, you can skip it, or raise an exception.

      -- Paul McGuire
      Austin, TX
      [color=blue]
      >
      > This *should* work, although I didn't test it. Also, it assumes that[/color]
      recursive[color=blue]
      > function calls share the same list ('data'). It would be cleaner to have
      > import_file create, fill and return its own list. In pseudocode:
      >
      > def import_file(fil ename):
      > data = []
      > ...
      > for line in lines:
      > if (pattern matches):
      > z = import_file(fil ename_from_patt ern)
      > data.extend(z)
      > ...
      >
      > return data
      >
      >[/color]


      Comment

      Working...