Skip to content

Instantly share code, notes, and snippets.

@arthur-e
Created May 18, 2017 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arthur-e/c1b238ad50c0958ef32de0fc3cb22599 to your computer and use it in GitHub Desktop.
Save arthur-e/c1b238ad50c0958ef32de0fc3cb22599 to your computer and use it in GitHub Desktop.
An example Python script for the Software Carpentry Python lesson (https://github.com/arthur-e/swc-workshop/tree/master/python-climate)
'''
Reports the min and max July temperatures for each file
that matches the given filename pattern.
'''
import csv
import os
import sys
import glob
# Get the user-specified directory
directory = sys.argv[1]
# Pattern to use in searching for files
filename_pattern = os.path.join(directory, '*temperature.csv')
for filename in glob.glob(filename_pattern):
july_temps = []
# While the file is open...
with open(filename, 'r') as stream:
# Use a function to read the file
reader = csv.reader(stream)
# Each row is a year
for row in reader:
# Add this year's July temperature to the list
july_temps.append(row[6])
# A human-readable name for the file
pretty_name = os.path.basename(filename)
print(pretty_name, '--Hottest July mean temp. was', max(july_temps), 'deg K')
print(pretty_name, '--Coolest July mean temp. was', min(july_temps), 'deg K')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment