Someone who I work with is sad that it’s got to manually insert some movies into a database. So I wanted to help him building a fast solution he must only rename the files like this
description1_description2_test_lala.mp4
He runs the python script and it will automatically build a sql file with INSERT stataments
I really like python for small tasks,anything than the old braindead C
The script is this :
| Python | | copy code | | ? |
| 01 | import glob |
| 02 | import os |
| 03 | import sys |
| 04 | |
| 05 | li = [] |
| 06 | print """/ |
| 07 | |
| 08 | SQL GEN by opc0de 2013 |
| 09 | |
| 10 | """ |
| 11 | folder = raw_input('Enter folder ->') |
| 12 | tabela = raw_input('Tabela ->') |
| 13 | campuri = raw_input('Campuri ->') |
| 14 | if os.path.isdir(folder): |
| 15 | print "Directory exists ... reading file names" |
| 16 | else: |
| 17 | sys.exit("Invalid directory ... exiting") |
| 18 | folder = folder + '/*.*' |
| 19 | li = glob.glob(folder) |
| 20 | dump = open('dump.sql','w+') |
| 21 | for s in li: |
| 22 | count = len(folder) - 3 |
| 23 | s = s[count:] |
| 24 | fname = s |
| 25 | s = s[:s.rfind('.')] |
| 26 | lista = s.split('_') |
| 27 | for j in range(len(lista)): |
| 28 | lista[j] = "\"" + lista[j] + "\"" |
| 29 | linie = "INSERT INTO " + tabela + "(" + campuri + ") VALUES(" |
| 30 | conc = ",".join(lista) |
| 31 | linie = linie + conc +",\"" + fname + "\")" |
| 32 | dump.write(linie + "\n") |
| 33 | print "Dump complete checkout dump.sql!" |
| 34 |
