Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python/Pandas中创建新文件夹/目录_Python_Python 2.7_Pandas - Fatal编程技术网

在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
。您的代码中可能有重复的。根本没有尝试创建目录…虽然它是在不存在的情况下自动创建的。