visual studio – python directory not read correctly in os.listdir()


I have a script in python where I want to read in a path to a directory in order to read in the files within this directory.
When debugging it throws me an error.

When I specify the path within the function it all works, however if I specify it outside the function it throws me an error

path = "users/folder"
def read_data(path):
   directory_csv = path
   for filename in os.listdir(path): # here it throws me the error
      if filename.endswith('.csv'):
         file_path = os.path.join(path, filename)

error:

Exception has occurred: TypeError
listdir: path should be string, bytes, os.PathLike, integer or None, not DataFrame

However if I specify it within the function it works

def read_data(path):
path = "users/folder"
   directory_csv = path
   for filename in os.listdir(path): # now it works
      if filename.endswith('.csv'):
         file_path = os.path.join(path, filename)

I have already tried a workaround with

for filename in os.listdir(str(path)): #specifying it as string 

but it does not work

Leave a Reply

Your email address will not be published. Required fields are marked *