Python Forum
File loop curiously skipping files - FIXED
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File loop curiously skipping files - FIXED
#1
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" )
Reply


Messages In This Thread
File loop curiously skipping files - FIXED - by mbk34 - Feb-08-2024, 09:53 PM
RE: File loop curiously skipping files - by mbk34 - Feb-09-2024, 09:13 AM
RE: File loop curiously skipping files - by mbk34 - Feb-09-2024, 09:32 PM
RE: File loop curiously skipping files - by mbk34 - Feb-09-2024, 09:32 PM
RE: File loop curiously skipping files - by mbk34 - Feb-09-2024, 12:54 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Loop through directories and files one level down? Winfried 3 331 Apr-28-2024, 02:31 PM
Last Post: Gribouillis
  Loop through all files in a directory? Winfried 10 611 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  How to do "fixed size" (wrapping) math in Python? AlexanderWulf 13 2,075 Jul-19-2023, 04:13 PM
Last Post: deanhystad
  Fixed colum width for rowLabels i Matplotlib pandabay 0 454 Jun-10-2023, 03:40 PM
Last Post: pandabay
  How to loop through all excel files and sheets in folder jadelola 1 4,634 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Skipping line in text without Restarting Loop IdMineThat 4 1,552 Apr-05-2022, 04:23 AM
Last Post: deanhystad
  Encrypt and decrypt in python using own fixed key SriRajesh 3 5,123 Feb-20-2022, 01:18 PM
Last Post: dboxall123
  python seems to be skipping lines of code alansandbucket 1 4,236 Jun-22-2021, 01:18 AM
Last Post: Larz60+
  Play fixed frequency sound in python 3 jpezz 2 2,850 Feb-07-2021, 08:21 PM
Last Post: jpezz
  Referencing a fixed cell Mark17 2 2,120 Dec-17-2020, 07:14 PM
Last Post: Mark17

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020