I'm trying to create a script that will upload everyday some files to our Postgres DB.
I create a python script that reads all the .csv files in a given directory:
# -*- coding: utf-8 -*-
import os
import psycopg2
connect_string = "dbname='db' user='postgres' host='localhost' password='########"
conn = psycopg2.connect(connect_string)
cur = conn.cursor()
folder = u"SomePathOnTheServer\\Dépôt Chronos"
os.chdir(folder)
listFiles = os.listdir(".")
for files in listFiles:
if files.startswith("[Prefix]"):
if files.endswith(".csv"):
full_path = os.path.join(folder, files)
print full_path
cur.execute(u"""SET client_encoding to 'latin1';
COPY sde.assignation_train FROM '%s' DELIMITER ';' CSV HEADER;""" %(full_path))
cur.close()
print "All good!"
The problem is that the folder has some accents: "/Dépôt Chronos". I can't change that since the folder is automatically generated (and I only have "read" access to the folder).
When I print my full path, it's correct with all caracters (marked in green). But it seems that the path that's passed to my "cur.execute" is not. I tried adding an u""" """ before my string to pass it as unicode, but it's not working.
Any idea what's wrong?
Thanks!!!