sync databse table based on current directory data without losign previous values

Νίκος Γκρ33κ nikos.gr33k at gmail.com
Wed Mar 6 08:45:57 CET 2013


I'am using this snipper to read a current directory and insert all filenames into a databse and then display them.

But what happens when files are get removed form the directory?
The inserted records into databse remain.
How can i update  the databse to only contain the existing filenames without losing the previous stored data?

Here is what i ahve so far:

==================================
path = "/home/nikos/public_html/data/files/"

#read the containing folder and insert new filenames
for result in os.walk(path):
	for filename in result[2]:
		try:
			#find the needed counter for the page URL
			cur.execute('''SELECT URL FROM files WHERE URL = %s''', (filename,) ) 
			data = cur.fetchone()        #URL is unique, so should only be one
			
			if not data:
				#first time for file; primary key is automatic, hit is defaulted
				cur.execute('''INSERT INTO files (URL, host, lastvisit) VALUES (%s, %s, %s)''', (filename, host, date) )
		except MySQLdb.Error, e:
			print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
======================

Thank you.



More information about the Python-list mailing list