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
  an error while copying code astral_travel 9 193 5 hours ago
Last Post: astral_travel
  Copying files if the name is in range tester_V 9 242 Nov-24-2022, 12:21 AM
Last Post: tester_V
  Find (each) element from a list in a file tester_V 3 281 Nov-15-2022, 08:40 PM
Last Post: tester_V
  pyPDF2 PDFMerger close pensding file japo85 2 562 Jul-28-2022, 09:49 AM
Last Post: japo85
  what will be the best way to find data in txt file? korenron 2 421 Jul-25-2022, 10:03 AM
Last Post: korenron
  Find and Replace numbers in String giddyhead 2 504 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Close and Open QDialog from QDialog 2 ITSRL 4 511 Jul-12-2022, 12:35 AM
Last Post: ITSRL
  find some word in text list file and a bit change to them RolanRoll 3 545 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 637 May-06-2022, 01:44 PM
Last Post: Larz60+
  Find and delete above a certain line in text file cubangt 12 1,493 Mar-18-2022, 07:49 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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