2016-10-11 3 views
-1

Ich schrieb ein Programm, um einige csv-Dateien vertikal zu verketten. Hier ist das Programm."Fehler 21: Ist ein Verzeichnis" beim Ausführen eines verketteten Skripts

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) 

Allerdings, wenn ich mein Programm testen, gibt es einen kleinen Fehler. Hier sind meine Eingaben.

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' 

Wie würde ich das beheben? Ich denke, es könnte die Art sein, wie ich das Verzeichnis eingegeben habe? Danke

Antwort

1

der Fehler sagen so ziemlich alles outfile sollte ein Pfad zu einer Datei, nicht Verzeichnis sein. Anstatt also dieses

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

tun:

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

O Ich verstehe. Also, indem ich os, path.join mache, kann ich einen Pfad zu einer Datei machen. Vielen Dank! –

Verwandte Themen