Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9283525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:39:11+00:00 2026-06-18T18:39:11+00:00

I wanted a script that iterates through csv files in a folder and dump

  • 0

I wanted a script that iterates through csv files in a folder and dump them into a MySQL database. I was able to dump one csv file into it.. But have troubles passing the file name in to the SQL script.

This is the code I use

file_path="C:\csv-files"
files=os.listdir(file_path)
files.sort()
for n in files:

    cursor.execute(" LOAD DATA LOCAL INFILE '%s' INTO TABLE new_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' Lines terminated by '\n' IGNORE 1 LINES ",(n))

And I get the following error

raise errorclass, errorvalue
ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'file1.csv'' INTO TABLE new_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY' at line 1")

If I use the file name directly instead of passing it, it works fine.

If you can see in the error thrown out, there seems to be an error in the SQL Script.

This would be the whole code

import csv
import MySQLdb
import sys
import os
connection = MySQLdb.connect(host='localhost',
    user='root',
    passwd='password',
    db='some_db')


cursor = connection.cursor()

file_path="C:\csv-files"
files=os.listdir(file_path)
files.sort()
for n in files:
    print n



    cursor.execute(" LOAD DATA LOCAL INFILE %s INTO TABLE new_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '"' Lines terminated by '\n' IGNORE 1 LINES " %n)



connection.commit()
cursor.close()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T18:39:12+00:00Added an answer on June 18, 2026 at 6:39 pm

    First, replace ‘%s’ with %s in the query. MySQLdb handles any quoting automatically.

    Here’s the code with some corrections and changes:

    import MySQLdb
    import os
    
    CSV_DIR = "C:\csv-files"
    
    connection = MySQLdb.connect(host='localhost',
                                 user='root',
                                 passwd='password',
                                 db='some_db',
                                 local_infile=1)
    
    cursor = connection.cursor()
    
    try:
        for filename in sorted(os.listdir(CSV_DIR)):
            cursor.execute("""LOAD DATA LOCAL INFILE %s
                              INTO TABLE new_table
                              FIELDS
                              TERMINATED BY ','
                              OPTIONALLY ENCLOSED BY '"'
                              ESCAPED BY '"'
                              LINES TERMINATED BY '\n'
                              IGNORE 1 LINES""",
                          (os.path.join(CSV_DIR, filename),))
    
        connection.commit()
    finally:
        cursor.close()
    

    NOTE: I set local_infile parameter to 1 in MySQLdb.connect and pass filename in tuple to execute.

    Works for me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted a script that automatically generates thumbnails when from a folder full of
I'm trying to write a DML script that updates a column but I wanted
I wanted to configure a script that would basically take the opening Outlook 2010
I wanted to write a small script that searched for an exact file name,
I´m totally new to Powershell and wanted to write a script that deletes all
I have a cron script that needs to write some html into a file.
I was trying to create a quick little script that would insert data into
A silly question maybe but I wanted clarification. I've created a script that has
I wanted to hack a quick script that simply splits a nucleotide sequence (a
I am new to C++ coding. I wanted to execute a Perl script (that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.