pythonloopsfilegoogle-colaboratorydrive

Loops in blocks of files, where for every 10 files, I have to do something


I am using google colab, and I have the files in my drive like this:

M0000.csv
M0001.csv
M0002.csv
.
.
.
M0099.csv

I need to loop in 100 files, where for every 10 files, I have to do something. I need to save all text in 10 files in 1 list array to be like:

all_text[0] = list of text in file from 1 to 10
.
.
all_text[9] = list of text in file from 91 to 100

Here is my code for looping in all files (without looping for each 10--I don't know how):

dir = 'drive/My Drive/Tri/'

pd.options.display.max_colwidth = 5000
#Loop for all file
for file in sorted(glob.glob(dir + "*.csv")):
  print(f"File: {file}")
  # Check the number of columns in the file
  df = pd.read_fwf(file, header=None, on_bad_lines='skip', delimiter="\n")
  
  # Loop inside each file
  for i in range(len(df)): # Loop over the rows ('i')
      
      #code to do

  print("All  Text:", all_text)

Solution

  • Keep track of how many files you have processed. If that number is divisible by ten, do your extra thing.

    filenumber = 0
    for file in sorted(glob.glob(dir + "*.csv")):
        filenumber += 1
        if filenumber % 10 == 0:
            # do your extra thing