-1

Want to create a new folder, getting an error.

Method:

def IDW_to_df(conn, quarter, file_name,sql_statement, *columns):
cursor = conn.cursor()
cursor.execute(sql_statement)
Dict = {}
for column in columns:            
    Dict[column]=[]
while 1:
    row = cursor.fetchone()
    if not row:
        break
    x = 0
    for column in columns:
        Dict[column].append(row[x])
        x += 1
df = pd.DataFrame(Dict)
df.to_csv('H:/Q{0}/{1}.csv'.format(quarter,file_name))
return df   

The method itself functions properly, just the folder creation throws the error. When called, I get the following error.

IOError: [Errno 2] No such file or directory: 'H:/Q4/FOO_IND.csv'
nonegiven72
  • 407
  • 6
  • 17

1 Answers1

0

Thanks, had to create directory first. Will probably add some checks in the future to ensure the "new" directory doesn't already exist.

import pyodbc
import pandas as pd
import os
def IDW_to_df(conn, quarter, file_name,sql_statement, *columns):
    cursor = conn.cursor()
    cursor.execute(sql_statement)
    Dict = {}
    for column in columns:            
        Dict[column]=[]
    while 1:
        row = cursor.fetchone()
        if not row:
            break
        x = 0
        for column in columns:
            Dict[column].append(row[x])
            x += 1
    df = pd.DataFrame(Dict)
    os.makedirs('H:/Q{0}'.format(quarter))
    df.to_csv('H:/Q{0}/{1}.csv'.format(quarter,file_name))
    return df   
nonegiven72
  • 407
  • 6
  • 17