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
  pyPDF2 PDFMerger close pensding file japo85 2 219 Jul-28-2022, 09:49 AM
Last Post: japo85
  what will be the best way to find data in txt file? korenron 2 176 Jul-25-2022, 10:03 AM
Last Post: korenron
  Find and Replace numbers in String giddyhead 2 275 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  Close and Open QDialog from QDialog 2 ITSRL 4 286 Jul-12-2022, 12:35 AM
Last Post: ITSRL
  find some word in text list file and a bit change to them RolanRoll 3 324 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 350 May-06-2022, 01:44 PM
Last Post: Larz60+
  Why is copying and pasting a block now broken? WagmoreBarkless 2 413 May-05-2022, 05:01 AM
Last Post: WagmoreBarkless
  Why is copying and pasting a block now broken? WagmoreBarkless 1 383 May-04-2022, 11:40 PM
Last Post: Larz60+
  Find and delete above a certain line in text file cubangt 12 1,159 Mar-18-2022, 07:49 PM
Last Post: snippsat
  Facing Problem while opening a file through command prompt vlearner 4 782 Jan-30-2022, 08:10 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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