Python Forum
How To Find an Opening and Closing String, Copying Open/Close/Contents to New File
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How To Find an Opening and Closing String, Copying Open/Close/Contents to New File
Hi All,

I want to use Python to parse some old BASIC code. Basically, I'm looking to locate each subroutine within the source file and then copy each subroutine out into a new file named after the subroutine.

I imagine it would go something like this:
1. Find a string containing sub nameofsub
2. Find the matching end sub for this subroutine
3. Create a new file with the name of the subroutine.
4. Copy the contents of the opening and closing strings and all contained text into this new file.
5. Repeat from the beginning.
Still working through this, I've posted my code to github:

Right now it:
1. Loads the file into a list.
2. Iterates through each item in the list.
3. If it finds the string "DECLARE SUB" outputs a message to the display.
4. Continues parsing until the entire list (and thus contents of the file) have been searched.

About as far as I'm getting with it tonight...will update as I make more progress. Any suggestions are welcome :)

# Python BASIC Tools

# Empty List to Contain Lines of BASIC Code
baslist = []

# Open Source File and Print Contents
with open('/workspaces/python/pyBASICTools/CWSTRAT.BAS', 'rt') as basfile:
    # Copy each line into baslist.
    for line in basfile:

# Search for instances of SUB
index = 0 # current index in list
prev = 0 # previous index in list

while index < len(baslist):
    lineindex = 0 # current index in string
    lineprev = 0 # previous index in string
    str = baslist[index] # first string to search from baslines
    substr = "SUB animate" # substring we are looking for
    while lineindex < len(str): # While there are still more items in the list
        found = str.find(substr, lineindex)
        if found == -1:
            lineindex = lineindex + 1
        print(" " * (found - lineprev) + "e", end='') # Print location of substring
        lineprev = found + len(substr) 
        lineindex += len(substr)
    prev = index
    index = index + 1

Possibly Related Threads…
Thread Author Replies Views Last Post
  my python file closing rwahdan 1 267 Jun-30-2021, 08:25 AM
Last Post: Gribouillis
  Open an excel file Newbie1114 1 335 Jun-16-2021, 09:11 PM
Last Post: Gribouillis
  How to open MIDI-file and get events in a list? philipbergwerf 7 1,408 May-29-2021, 08:24 AM
Last Post: j.crater
  Find string between two substrings, in a stream of data xbit 1 416 May-09-2021, 03:32 PM
Last Post: bowlofred
Music XML File - cannot find the tag ateestructural 1 475 Apr-06-2021, 08:26 AM
Last Post: ibreeden
  Error on open of file created with tempfile.TemporaryDirectory() Brian177 4 786 Apr-05-2021, 07:12 PM
Last Post: Brian177
  I can't open a python file :( Oshadha 2 679 Mar-28-2021, 11:00 PM
Last Post: pythonprogrammer1101935
  find the header location in a .bin file without reading the whole file at a time SANJIB 0 470 Mar-05-2021, 04:08 PM
Last Post: SANJIB
  Subprocesses not opening File Select Dialog teut 2 417 Feb-22-2021, 08:07 PM
Last Post: teut
  Find specific file in an archive tester_V 8 833 Feb-13-2021, 06:13 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020