title: Adding lines from a text file and printing out result using a for loop - Python
body: <p><strong>Problem</strong></p>
  <p>I have a set of 50 files, which contain 8192 lines of integers (after skipping the first 12, which are irrelevant, there are also 12 more lines at the bottom which can be skipped). I am only interested in a patch of 70 lines in each file (lines 745-815, or 757-827 if you include the 12 at the start). The files have a naming pattern of 'DECAY_COINC000.Spe','DECAY_COINC001.Spe' etc. Desired output below.</p>
  <p><strong>Existing Code (Minimalised Example)</strong></p>
  <pre><code>import numpy as np
  from numpy import exp, loadtxt, pi, sqrt, random, linspace
  import glob, os
  
  ## Define constants
  fileToRun = 'Run0'
  location = 'ControlRoom6'
  approxcen = 780
  DeadTime = 3
  LiveTime = 15
  width = 1
  
  ## Get location of files to be loaded
  folderId = '\\'
  baseFolder = 'C:'+folderId+'Users'+folderId+location+folderId+'Documents'+folderId+'PhD'+folderId+'Ubuntu-Analysis-DCF'+folderId+'DCF-an-b+decay'+folderId+'dcp-ap-27Al'+folderId+''
  prefix = 'DECAY_COINC'
  
  ## Define paramaters
  folderToAnalyze = baseFolder + fileToRun + '\\'
  MaestroT = LiveTime + DeadTime
  definiteintegralprints = 1
  
  ## Gets number of files
  files = []
  os.chdir(folderToAnalyze)
  for file in glob.glob(prefix + &quot;*.Spe&quot;):
      files.append(file)
  numfiles = len(files)
  if numfiles&lt;=1:
      print('numfiles is {0}, minimum of 2 is required'.format(numfiles))
      raise SystemExit(0)
  
  if definiteintegralprints == 1:   
      print(&quot;Commence printing counts in fixed interval sequence&quot;)
      
      xmin = 745
      xmax = 815
      
      for n in range(0, numfiles):
      
          x = np.linspace(0, 8191, 8192)
          finalprefix = str(n).zfill(3)
          fullprefix = folderToAnalyze + prefix + finalprefix
          y = loadtxt(fullprefix + &quot;.Spe&quot;, skiprows= xmin+12, max_rows = xmax-xmin) 
      
          
          for x in range(xmin,xmax):
              count = count + y
              time = MaestroT*(n+1)
              
              print(time, count)
  </code></pre>
  <p><strong>Desired Output</strong></p>
  <p>The target is to add the number on each of those lines to find the total count between xmin and xmax, and print out, alongside with a number to designate which file it came from (here I use 18 * number of  the file+1).</p>
  <p>So, an output that looks like this, would be fine:
  (18, 88), (36,73), (54,66), (72,55)...</p>
  <p><strong>Current Output</strong></p>
  <pre><code>Commence printing counts in fixed interval sequence
  18 [   0.  210.    0.  281.   70.  141.   70.  490.  140.  280.  351.  700.
    700.  491.  912.  492.  561.  633.  632.  630.  912. 1473. 1401. 1191.
   1260. 1262. 1401.  981. 1542. 1260. 1682. 1122. 1960. 1263. 2241. 1821.
   2102. 1471. 2242. 2103. 1471. 1892. 1403. 1822. 1400. 1331.  983. 1333.
    910. 1262.  980.  702.  772.  702.  700.  212.  561.  351.  843.  490.
    140.  280.  140.    0.   70.  210.    0.    0.  140.   70.]
  18 [   0.  210.    0.  282.   70.  142.   70.  490.  140.  280.  352.  700.
    700.  492.  914.  494.  562.  636.  634.  630.  914. 1476. 1402. 1192.
   1260. 1264. 1402.  982. 1544. 1260. 1684. 1124. 1960. 1266. 2242. 1822.
   2104. 1472. 2244. 2106. 1472. 1894. 1406. 1824. 1400. 1332.  986. 1336.
    910. 1264.  980.  704.  774.  704.  700.  214.  562.  352.  846.  490.
    140.  280.  140.    0.   70.  210.    0.    0.  140.   70.]
  18 [   0.  210.    0.  283.   70.  143.   70.  490.  140.  280.  353.  700.
    700.  493.  916.  496.  563.  639.  636.  630.  916. 1479. 1403. 1193.
   1260. 1266. 1403.  983. 1546. 1260. 1686. 1126. 1960. 1269. 2243. 1823.
   2106. 1473. 2246. 2109. 1473. 1896. 1409. 1826. 1400. 1333.  989. 1339.
    910. 1266.  980.  706.  776.  706.  700.  216.  563.  353.  849.  490.
    140.  280.  140.    0.   70.  210.    0.    0.  140.   70.]
  </code></pre>
  <p>...This goes on for many many lines. I don't really understand what's going on in the output if I am honest.</p>