0

I have a directory of CSV files that I want to import into MySQL. There are about 100 files, and doing a manual import is painful.

My command line is this:

mysqlimport -u root -ppassword --local --fields-terminated-by="|" data PUBACC_FR.dat

The files are all of type XX.dat, i.e. AC.dat, CP.dat, etc. I actually rename them first before processing them (via rename 's/^/PUBACC_/' *.dat). Ideally I'd like to be able to accomplish both tasks in one script: Rename the files, then run the command.

From what I've found reading, something like this:

for filename in os.listdir("."):
    if filename.endswith("dat"):
        os.rename(filename, filename[7:])

Can anyone help me get started with a script that will accomplish this, please? Read the file names, rename them, then for each one run the mysqlimport command?

Thanks!

itsjeyd
  • 5,070
  • 2
  • 30
  • 49
Mark Sinford
  • 135
  • 1
  • 8
  • possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Steve P. Oct 19 '13 at 15:14
  • 1
    I wouldn't do both tasks in the same script. – Benjamin Toueg Oct 19 '13 at 15:28
  • Any reason why you want to do that in Python ? You can simply run `ls *dat|xargs -n 1 mysqlimport -u root -ppassword --local --fields-terminated-by="|" data` in your shell. – damienfrancois Oct 19 '13 at 17:58

1 Answers1

1

I suppose something like the python code below could be used:

import subprocess
import os


if __name__ == "__main__":
    for f in os.listdir("."):
        if (f.endswith(".dat")):
            subprocess.call("echo %s" % f, shell=True)

Obviously, you should change the command from echo to your command instead.

See http://docs.python.org/2/library/subprocess.html for more details of using subprocess, or see the possible duplicate.

Xaldew
  • 560
  • 5
  • 18