在Python/Pandas中创建新文件夹/目录
要创建新文件夹,出现错误 方法:在Python/Pandas中创建新文件夹/目录,python,python-2.7,pandas,Python,Python 2.7,Pandas,要创建新文件夹,出现错误 方法: 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 ro
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
方法本身运行正常,只是文件夹创建抛出错误。调用时,我得到以下错误
IOError: [Errno 2] No such file or directory: 'H:/Q4/FOO_IND.csv'
谢谢,我必须先创建目录。将来可能会添加一些检查,以确保“新”目录不存在
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
检查您是否有目录
H:/Q4
。您的代码中可能有重复的。根本没有尝试创建目录…虽然它是在不存在的情况下自动创建的。