Benjamin Lu

everyday is cool

Counting Website Pages with Python

Written retrospectively for the 2003 archive.

Python 2.3 was released in July 2003. A little file-counting script is a way to revisit the kind of site maintenance task that was useful then. Run this version with a current Python installation:

import os

count = 0
for name in os.listdir("."):
    if name.lower().endswith(".html") and os.path.isfile(name):
        count += 1
print(count)

Run it in a folder with two HTML files and one image. You should get 2. Add an HTML file inside a subfolder and run it again. The count stays the same because the script only checks the current directory. That gives you a concrete next exercise if you want a count for the entire site.

Python 2.3 release