I have a .py file with text in it:
To be, or not to be: that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by opposing end them? To die: to sleep;
Next, I wrote this python script, which basically takes the above file (testFrom.py), adds the line number followed by a "> ", and finally adds triple quotes to then create a new file (testTo.py) and writes the new text to it. Here is the script:
inFileName = input("Enter the name of the first file: ")
outFileName = input("Enter the name of the second file: ")
lineCount = 0
newLines = ""
with open(inFileName, 'r') as readFile:
readData = readFile.readlines() # create a list of each line
for line in readData: # iterate through each line
lineCount += 1
if line == readData[0]:
newLines += "~" + str(lineCount) + "> " + str(line)
elif line == readData[len(readData) - 1]:
newLines += str(lineCount) + "> " + str(line) + "~"
else:
newLines += str(lineCount) + "> " + str(line)
with open(outFileName, 'w') as newFile:
newFile.write(newLines.replace("~", "\"\"\"")) # write this to new file
print(type(newLines))
readFile.close()
newFile.close()
The script creates the following file:
"""1> To be, or not to be: that is the question:
2> Whether 'tis nobler in the mind to suffer
3> The slings and arrows of outrageous fortune,
4> Or to take arms against a sea of troubles,
5> And by opposing end them? To die: to sleep;"""
I use a seperate python script to test if the original script did its job:
org_contents = """1> To be, or not to be: that is the question:
2> Whether 'tis nobler in the mind to suffer
3> The slings and arrows of outrageous fortune,
4> Or to take arms against a sea of troubles,
5> And by opposing end them? To die: to sleep;"""
with open('testTo.py', 'r') as f:
contents = f.read()
print(contents)
assert(org_contents == contents)
f.close()
And the above script gives this result:
/usr/local/bin/python3 /Users/Parent/Documents/modify/CodeChecker.py
"""1> To be, or not to be: that is the question:
2> Whether 'tis nobler in the mind to suffer
3> The slings and arrows of outrageous fortune,
4> Or to take arms against a sea of troubles,
5> And by opposing end them? To die: to sleep;"""
Traceback (most recent call last):
File "/Users/Parent/Documents/modify/CodeChecker.py", line 9, in <module>
assert(org_contents == contents)
AssertionError
I have to use the assert statement to test it because it is a school project that requires that. I understand that assert(org_contents == contents) is a logic tester. The code will move to the next line if the statement is True but will give the assertion error otherwise. Does anyone have any idea why it is giving an assertion error?
-and thanks in advance for reading my long post lol