-3

There are lot of csv excel sheets in a folder. All excel sheet contains data only in first 3 columns. I will select corresponding csv sheet from a lot of csv sheets and then plot it.Here is the code

import os
path = "F:\\Users\\Desktop\\Data\\Summary"
files = []
folder_data = os.listdir(path)
folder_data = [i+"\\" for i in folder_data]

# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        if '.csv' in file:
            files.append(file)
for i, f in enumerate(files):
    print(( i,f))
    print('\n'.join(f'{i}-{v}' for i,v in enumerate(files)))
    csv_code = str(int(input("Enter corresponding code to plot: ")))
    csv_path = path + "\\" + folder_data[csv_code] 

    df = pd.read_csv(csv_path, header=None)
    df1 = df.iloc[:,0:2]
    plt.plot(df1[0], df1[1])

When i run the code i want the Output to be displayed as follows (i mean i want all csv files from the folder to be displayed so that i can select what i want):

0-Test_Summary_1.csv
1-Test_Summary_2.csv
2-Test_Summary_3.csv
3-Test_Summary_4.csv
4-Test_Summary_5.csv
5-Test_Summary_6.csv etc

The error i a getting is

FileNotFoundError: 

In spite of csv file is there in the folder. I am getting error as File not found error

num3ri
  • 822
  • 16
  • 20
Anand Patil
  • 37
  • 10

1 Answers1

1

If I understand your question correctly, then you could try something like this:

import os
import pandas as pd

# see this answer about absolute paths in windows
# https://stackoverflow.com/a/7767925/9225671
base_path = os.path.join('f:', os.sep, 'Users', 'Desktop', 'Data', 'Summary')

# collect all CSV files in 'base_path' and its subfolders
csv_file_list = []
for dir_path, _, file_name_list in os.walk(base_path):
    for file_name in file_name_list:
        if file_name.endswith('.csv'):
            # add full path to the list, not just 'file_name'
            csv_file_list.append(
                os.path.join(dir_path, file_name))

print('CSV files that were found:')
for i, file_path in enumerate(csv_file_list):
    print('   {:3d} {}'.format(i, file_path))

selected_i = int(input('Enter corresponding number of the file to plot: '))
selected_file_path = csv_file_list[selected_i]
print('selected_file_path:', selected_file_path)

df = pd.read_csv(selected_file_path, header=None)
...

Does this work for you?

Ralf
  • 16,086
  • 4
  • 44
  • 68
  • When i run this code All csv files from the folder are not displayed. If all csv files displays i can select corresponding code to plot it – Anand Patil May 30 '19 at 12:02
  • @PriyankaMishra I don't understand the comment. What part of the code is it not working? Or what did you expect differently? – Ralf May 30 '19 at 12:05
  • When i run the code i want the output to be displayed as ```0-Test_Summary_1.csv 1-Test_Summary_2.csv 2-Test_Summary_3.csv 3-Test_Summary_4.csv 4-Test_Summary_5.csv 5-Test_Summary_6.csv etc``` so that i can select corresponding code to plot – Anand Patil May 30 '19 at 12:08
  • ```print('CSV files that were found:')``` all csv files in the folder are not displayed – Anand Patil May 30 '19 at 12:09
  • @PriyankaMishra if you add `print(file_name)` before the line `if file_name.endswith('.csv'):`, do the files appear that you want? – Ralf May 30 '19 at 12:15
  • if i add print(file_name) none of the files appeared – Anand Patil May 30 '19 at 12:19
  • @PriyankaMishra if you do `print(base_path)` does it show the correct folder? – Ralf May 30 '19 at 12:20
  • @PriyankaMishra well then correct it so it points to the correct folder – Ralf May 30 '19 at 12:22