Q:

Please explain my logical error while parsing .html files using basic Python

I have a script which runs and should produce a number of new .html files within a directory. It exits with code 0 indicating no issues and as far as I can tell SHOULD work correctly. It doesn't though! :)
The code should iterate over a directory of .html files and find all of the text between two elements in a HTML file (these elements are stored in the variables start and end).
import os

dir = os.listdir("C:/Users/folder")

files = []

for file in dir:
    if file[-5:] == '.html':
        files.insert(0, file)

for fileName in files:
    print fileName
    file = open("C:/Users/folder/" + fileName)
    content = file.read()
    file.close()

    start = content.find('<div class="title">')
    end = content.find('<div class="footer">')

    if start != -1:
        newContent = content[start:]
    if end != -1:
        newContent = content[0:end - 1]

    file = open(fileName + "_mod", 'w')
    file.write(newContent)
    file.close()

So this should iterate over a directory, find the text between '<div class="title">' and '<div class="footer">', save this text in a variable which is put into a new file which has the same name as the original with "_mod" appended to the end.
This not how it behaves, however. Instead, it finds from the beginning of the document up until the footer div. 
So I want it to convert a given html file from :
<head>
   <title>This is bad HTML</title>
</head>
<body>
  <h1> Remove me</h1>
  <div class="title">
    <h1> This is the good data, keep me</h1>

    <p> Keep this text </p>

  </div>
  <div class="footer">
    <h1> Remove me, I am pointless</h1>
  </div>
</body>

Into just : 
  <div class="title">
    <h1> This is the good data, keep me</h1>

    <p> Keep this text </p>

  </div>

But the output I am currently getting is : 
<head>
   <title>This is bad HTML</title>
</head>
<body>
  <h1> Remove me</h1>
  <div class="title">
    <h1> This is the good data, keep me</h1>

    <p> Keep this text </p>

  </div>

What logical errors have I made here?

A:

Your flaw lies in:
if start != -1:
    newContent = content[start:]
if end != -1:
    newContent = content[0:end - 1]

if start!=-1 and end != -1, newContent will only be content[0:end - 1]
You could do something like 
start = start if (start != -1) else 0
end = end if (end != -1) else len(content)

and then 
newContent = content[start:end -1]

