I'll admit I'm a beginner in python. I'm trying to write a small program to list all the media on my NAS device and write to an easy to read html file. It seems to work BUT the writing to the html file stops at a certain point without an error. The program continues looping round and no error messages are generated. The html file is about 60K so it's not huge. I wondered if it was a bad filename but, if I write a large text string to the file at the start of the program, the output finishes at an earlier file though the program still continues looping round. I'm using IDLE - does that have a limit on the number of lines written? I assume it must be a bug in my code but I can't see anything (fames last words). Any suggestions of where to look?
import os
import re
import time
# init things
startDir = 'Z:/Films'
cnt = 0
totalCnt = 0
now = time.time()
print( "Running" )
f = open( "films.html", "w" )
# head
f.write( "<html><head>" )
str = """
<style>
td {
background-color: #F0E68C;
font-size: 22px;
}
th {
background-color: #808080;
font-size: 26px;
}
</style>
</head>
"""
f.write( str )
# body
f.write( "<body style='background-color:#EEE8AA'><center>" )
# loop through directories
for root, dirs, filenames in os.walk( startDir ): # recursively goes through all dir
dir = root.split(os.path.sep)[-1] # path, split, one back
if dir == "Z:/Films": continue
# if dir == "SciFi": continue
# ignore the TV section
if "TV" in root: continue # should of moved above loop, multi line
str = "<table border=1 width=800 size=+1><tr><Th colspan=2><b>" + dir + "</b></th></tr>\n"
f.write( str )
left = True
cnt = 0
# loop through each file
for filename in filenames: # filen
# get film type
type = re.sub('.*[.]', '', filename )
# ignore if not video type
if type not in [ "avi", "mp4", "mkv", "m4v" ]:
continue
try:
col = "black"
path = os.path.join(root, filename)
size = os.stat(path).st_size
file = filename[:-4]
# highlight issues with files
if os.stat(path).st_mtime >= now - 30 * 86400:
col = "blue"
elif re.search("CD[2-9]", file):
continue
elif re.search("CD[0-9]", file):
col = "brown"
elif re.search("[12][09][0-9][0-9]", file) and not re.search("[(][12][09][0-9][0-9][)]", file):
col = "brown"
elif size > 1024 * 1024 * 1424:
col = "brown"
elif re.search("[^A-Za-z0-9 )(]", file):
col = "brown"
# produce 2 columns in html table
if left:
str = "<tr><td><font color=" + col + ">" + file + "</font></td>\n"
left = False
else:
str = "<td><font color=" + col + ">" + file + "</font></tr>\n"
left = True
f.write( str )
except Exception as e:
# say if any issues
print("issue ",filename)
print( e.args[0] )
cnt += 1
# say how many dealt with
print( " ",dir,cnt, "files" )
totalCnt += cnt
# finish table
if left: f.write( "<td></td></tr>" )
f.write( "</table><br><br>\n" )
# finish off
f.write( "</center></body></html>\n" )
str = f'There are {cnt} films'
f.write( str )
print( "Finished", totalCnt, "films" )
