I'm trying to write this function so that I can pass files or folders and read from them using pandas.
import pandas as pd
import os
path = os.getcwd()
path = '..' #this would be root
revenue_folder = '../Data/Revenue'
random_file = '2017-08-01_Aug.csv'
def csv_reader(csv_file):
for root, dirs, files in os.walk(path):
for f in files:
with open(os.path.join(root, csv_file)) as f1:
pd.read_csv(f1, sep = ';')
print(f1)
csv_reader(random_file)
FileNotFoundError: [Errno 2] No such file or directory: '../2017-08-01_Aug.csv'
I have since tried doing some changes and now the problem is that it goes to another subdirectory. What I want is to iterate through all my files and folders, find the desired file, then read it. To be clear my desired file is in the revenue_folder.
def csv_reader(csv_file):
for root, dirs, files in os.walk(path):
for f in files:
base, ext = os.path.splitext(f)
if ('csv' in ext):
print (root)
with open(os.path.join(root, csv_file)) as f1:
pd.read_excel(f1, sep = ':')
print(f1)
csv_reader(random_file)
FileNotFoundError: [Errno 2] No such file or directory: './Data/Backlog/2017-08-01_Aug.csv'