D
D
d1maYak0vlev2021-07-13 18:28:58
Python
d1maYak0vlev, 2021-07-13 18:28:58

How to solve TypeError: expected str, bytes or os.PathLike object, not list error?

You need to create a dataframe from csv files that are in folders from the archive

import os

# open a zip file
import zipfile
with zipfile.ZipFile('C:\DS\data\data.zip') as zip_ref:
zip_ref.extractall('C:\DS \data')

# define directories
path = os.listdir(f'C:\DS\data\data')

# create empty df
df = pd.DataFrame()

# read data from all files and append to df
for current_path, dirs , files in os.walk(path):
for file in files:
data_path = f'{current_path}/{file}'

temp_df=pd.read_csv(data_path)
df = pd.concat((df, temp_df))
print(df .shape)

Error: TypeError: expected str, bytes or os.PathLike object, not list

path yields
['2020-12-03',
'2020-12-04',
'2020-12-05',
'2020-12- 06',
'2020-12-07',
'2020-12-08',
'2020-12-09']

This is a list of folders from an archive that I unpacked

. How can I make this list readable?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-07-13
@d1maYak0vlev

os.walk(path)
os.walk() takes a path TO ONE directory, and you shove the list.
Just loop through path, and process each element already.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question