Skip to content

Instantly share code, notes, and snippets.

@nipunbatra
Created April 12, 2013 08:34
Show Gist options
  • Save nipunbatra/5370512 to your computer and use it in GitHub Desktop.
Save nipunbatra/5370512 to your computer and use it in GitHub Desktop.
import glob
import os
import time
import MySQLdb
DATA_PATH='/home/nipun/Desktop/'
THRESHOLD_TIME=300
connection = MySQLdb.Connect(host='', user='root', passwd='password', db='smart_meter_data',local_infile = 1)
cursor = connection.cursor()
folders=os.listdir(DATA_PATH)
try:
list_of_files=glob.glob(str(DATA_PATH)+str("/*.csv"))
for f in list_of_files:
if int(time.time())-int(os.stat(f).st_mtime)>THRESHOLD_TIME:
query = "LOAD DATA LOCAL INFILE "+"'"+f+"'"+" INTO TABLE data FIELDS TERMINATED BY ',' IGNORE 1 LINES"
cursor.execute(query)
connection.commit()
os.remove(f)
except MySQLdb.Error,e:
pass
else:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment