Read in all csv files from a directory using Python

后端 未结 4 929
傲寒
傲寒 2020-12-13 10:44

I hope this is not trivial but I am wondering the following:

If I have a specific folder with n csv files, how could I iteratively read all of

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 11:24

    I think you look for something like this

    import glob
    
    for file_name in glob.glob(directoryPath+'*.csv'):
        x = np.genfromtxt(file_name,delimiter=',')[:,2]
        # do your calculations
    

    Edit

    If you want to get all csv files from a folder (including subfolder) you could use subprocess instead of glob (note that this code only works on linux systems)

    import subprocess
    file_list = subprocess.check_output(['find',directoryPath,'-name','*.csv']).split('\n')[:-1]
    
    for i,file_name in enumerate(file_list):
        x = np.genfromtxt(file_name,delimiter=',')[:,2]
        # do your calculations
        # now you can use i as an index
    

    It first searches the folder and sub-folders for all file_names using the find command from the shell and applies your calculations afterwards.

提交回复
热议问题