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!

3
  • possible duplicate of Calling an external command in Python Commented Oct 19, 2013 at 15:14
  • 1
    I wouldn't do both tasks in the same script. Commented Oct 19, 2013 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. Commented Oct 19, 2013 at 17:58

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.