Skip to content

Instantly share code, notes, and snippets.

@bemasher
Last active August 29, 2015 14:01
Show Gist options
  • Save bemasher/12d41a328a3573fd99fa to your computer and use it in GitHub Desktop.
Save bemasher/12d41a328a3573fd99fa to your computer and use it in GitHub Desktop.
For all *.txt and *.awd files in the data folder, calculate the sum of each line across all files starting at the 20th value.
import glob
output = []
# For both extensions
for ext in ["txt", "awd"]:
# For all files matching the current extension
for filename in glob.iglob("data/*.{}".format(ext)):
print filename
# For each line in current file
for idx, line in enumerate(open(filename)):
# Skip first 19 lines
if idx < 19:
continue
# Array offset (arrays are 0-indexed)
offset = idx - 19
# If the output array isn't long enough, append an element
if len(output) <= offset:
output.append(0)
# Add current value to the output array at the current offset
output[offset] += int(line)
# Create the output file
with open("sums.txt", "w") as sums:
# Write all sums, one per line
for val in output:
print >>sums, val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment