Python &引用;错误21:是一个目录";运行连接脚本时

Python &引用;错误21:是一个目录";运行连接脚本时,python,csv,concatenation,Python,Csv,Concatenation,我编写了一个程序来垂直连接一些csv文件。这是节目单 import os import glob import pandas def concatenate(indir, outfile, colnames): os.chdir(indir) fileList=glob.glob('*.csv') dfList=[] for filename in fileList: print(filename) df=pandas.read_

我编写了一个程序来垂直连接一些csv文件。这是节目单

import os
import glob
import pandas

def concatenate(indir, outfile, colnames):
    os.chdir(indir)
    fileList=glob.glob('*.csv')
    dfList=[]
    for filename in fileList:
        print(filename)
        df=pandas.read_csv(filename,header=None)
        dfList.append(df)
    concatDf=pandas.concat(dfList,axis=0)
    concatDf.columns=colnames
    y=str(input("What do you want to name your file?"))
    concatDf.csv_path = y
    concatDf.to_csv(outfile,index=None)


def main():
    indir = str(input("What is the directory that you want to concatenate?" ))
    outfile = str(input("What is the directory that you want to export the merged file to?" ))
    string_input = input("What are the column names?: ")
    input_list = string_input.split()
    colnames = [str(x) for x in input_list]
    concatenate(indir, outfile, colnames)
然而,当我测试我的程序时,有一个小错误。这是我的意见

main()

What is the directory that you want to concatenate?/Users/hem/Desktop/Complete_Pilot_Copy/5555_1/DelayDiscounting

What is the directory that you want to export the merged file to?/Users/hem/Desktop/DelayedDiscountingAnalyzed

What are the column names?: Date    SubjectID   SessionID   ProtocolID  SiteID  TaskID  UserResponse    LogDiscountRate LogDiscountRateStd  QuestionRule    NegativeDiscountFlag    ZeroDiscountFlag    BozoDiscountFlag    gldomain    ProposedValue1  ProposedDelay1  ProposedValue2  ProposedDelay2  ProposedValue3  ProposedDelay3  TrialStartTimeSec   ResponseTimeSec
DDT_5555_1_HUBS071501_BU_062016_135920.csv
DDT_5555_1_HUBS071501_BU_062016_140010.csv
DDT_5555_1_HUBS071501_BU_062016_140051.csv
DelayedDiscounting_5555_1.csv

What do you want to name your file?5555_1DelayDiscounting
Traceback (most recent call last):

  File "<ipython-input-2-58ca95c5b364>", line 1, in <module>
    main()

  File "<ipython-input-1-867fad0a7568>", line 26, in main
    concatenate(indir, outfile, colnames)

  File "<ipython-input-1-867fad0a7568>", line 17, in concatenate
    concatDf.to_csv(outfile,index=None)

  File "/Users/hem/anaconda/lib/python3.5/site-packages/pandas/core/frame.py", line 1344, in to_csv
    formatter.save()

  File "/Users/hem/anaconda/lib/python3.5/site-packages/pandas/formats/format.py", line 1526, in save
    compression=self.compression)

  File "/Users/hem/anaconda/lib/python3.5/site-packages/pandas/io/common.py", line 424, in _get_handle
    f = open(path, mode, errors='replace')

IsADirectoryError: [Errno 21] Is a directory: '/Users/hem/Desktop/DelayedDiscountingAnalyzed'
main()
您要连接的目录是什么?/Users/hem/Desktop/Complete\u Pilot\u Copy/5555\u 1/DelayDiscounting
要将合并文件导出到哪个目录?/Users/hem/Desktop/DelayedDiscountingAnalyzed
列名是什么?:日期主体会话ID协议站点ID任务ID用户响应日志Decontrate LogDecontrateSTD问题规则否定搜索滞后零折扣滞后BozoDecontrateSTD gldomain建议值1建议延迟1建议值2建议延迟2建议值3建议延迟3 TrialStartTimeSec反应性
DDT_5555_1_HUBS071501_BU_062016_135920.csv
滴滴涕5555_1_HUBS071501_BU_062016_140010.csv
DDT_5555_1_HUBS071501_BU_062016_140051.csv
延迟结算_5555 _1.csv
您想将文件命名为什么?5555\u 1延迟计数
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
main()
文件“”,第26行,主
连接(indir、outfile、colnames)
文件“”,第17行,串联
concatDf.to_csv(输出文件,索引=无)
文件“/Users/hem/anaconda/lib/python3.5/site packages/pandas/core/frame.py”,第1344行,输入到csv
格式化程序。保存()
文件“/Users/hem/anaconda/lib/python3.5/site packages/pandas/formats/format.py”,第1526行,保存
压缩=自压缩)
文件“/Users/hem/anaconda/lib/python3.5/site packages/pandas/io/common.py”,第424行,在获取句柄中
f=打开(路径、模式、错误='replace')
IsDirectoryError:[Errno 21]是一个目录:'/Users/hem/Desktop/DelayedDiscountingAnalyzed'

我该如何解决这个问题?我想可能是我进入目录的方式吧?谢谢

这个错误说几乎所有的
outfile
都应该是文件的路径,而不是目录。所以不是这个

y=str(input("What do you want to name your file?"))
concatDf.csv_path = y
concatDf.to_csv(outfile,index=None)
这样做:

y=str(input("What do you want to name your file?"))
concatDf.to_csv(os.path.join(outfile, y),index=None)

哦,我明白了。因此,通过执行操作系统path.join,我可以使其成为文件的路径。非常感谢!