0

I have multiple pickle files in a folder and I want to first load them and then plot them. Indeed, I want to plot them based on their names. Hence, I need to load them into different data frames or dictionaries, then I plot them. For example, I need to use linear plot when the name of the files consist "linear" and use scatter plot when the names include 'scat'. I can read them individually and then plot them using the below code:

 import pickle
 with open('path/filename', 'rb') as f:
        mydata=pickle.load(f)
 f.close()

But the thing is these files are changing, something I have less files sometime more files. So I need to do in an automatic way. Any idea?

Spedo
  • 355
  • 3
  • 13

2 Answers2

1

You can use os.listdir() as described here to get a list of all files in a given folder.

You can then make a dictionary of file names (key) and data (value) (assuming that all files have unique names).

Ivo De Jong
  • 199
  • 7
1

First you need to list all the pickle files:

import glob

all_files = glob.glob('path_to_files/*.pickle')

Then you can check if the file names contain linear or scatter:

for curr_file in all_files:

   with open(curr_file, 'rb') as f:
        mydata=pickle.load(f)

        if curr_file.find('linear'):
            # code to plot_linear_plot
            # ...

        if curr_file.find('scatter'):
            # code plot_scatter_plot
            # ...

Iñigo González
  • 3,735
  • 1
  • 11
  • 27