1

I have two separate scripts currently, One that formats a single document and one that compares two formatted documents to look for differences. I'm trying to combine the two so it's more efficient. Any help is greatly appreciated.

To format the code within a document:

f1 = open("test4", "r")

f1_line = f1.readline()
line_no = 1

# Loop if either file1 or file2 has not reached EOF
while f1_line != '':

    for line in f1:
        if "001A" in line: print ((line))
        if "##!!STF" in line: print((line))

    #Increment line counter
        line_no += 1

f1.close()

Here is part of the next code that compares two formatted documents for differences:

# Ask the user to enter the names of files to compare
fname1 = input("Enter the first filename: ")
fname2 = input("Enter the second filename: ")

# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)

# Print confirmation
print("-----------------------------------")
print("Comparing files ", " > " + fname1, " < " +fname2, sep='\n')
print("-----------------------------------")

# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()

# Initialize counter for line number
line_no = 1

# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':

    # Strip the leading whitespaces
    f1_line = f1_line.rstrip()
    f2_line = f2_line.rstrip()

    a_string = f1_line
    substr1 = a_string[17:23]

    b_string = f2_line
    substr2 = b_string[17:23]

    if substr1 !=substr2:
        print("Line-%d" %  line_no,f1_line, f2_line)

        # Print a blank line
        print()

What I'm trying to figure out is how to combine both scripts together to ask the user to enter two unformatted documents, format them, compare and find differences between the two documents then display the results.

adam lawson
  • 21
  • 1
  • 3

1 Answers1

0

Seems like you have two options:

(1) Combine both into one file, where you separate them into separate functions. Then in your main, call each function appropriately.

(2) Create another file, which will call your two existing scripts. See here for some info on how to do this: What is the best way to call a script from another script?

Unless there's any reason to, I would just go with option 1.

kebab-case
  • 1,732
  • 1
  • 13
  • 24
  • I appreciate the quick response, and agree with you about just combining them into one file, I'm just not sure how to go about calling each functions separately. – adam lawson Jul 30 '19 at 19:03
  • @adamlawson See this link here https://www.w3schools.com/python/python_functions.asp It may be easier to start with hard-coding the file names instead of getting them from the user – kebab-case Jul 30 '19 at 20:01