Search code examples
pythonpandasfiledirectorypython-os

Finding the exact directory of the file in Python


Have a Pandas dataframe with the list of file names:

Files
3003.txt
3000.txt

Also having three directories in which I'd like to make search for these files:

dirnames=[
    '/Users/User/Desktop/base/reports1',
    '/Users/User/Desktop/base/reports2',
    '/Users/User/Desktop/base/reports3']

Doing like this:

for dirname in dirnames:
    for f in os.listdir(dirname):
        with open (os.path.join(dirname,f), "r", encoding="latin-1") as infile:

            if f==df.Files:

                text = infile.read() 
                print(f,dirname)

Gives the error

The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

How is it possible to do properly-rewrite the if conditions or make the for loop in another way?

Thanks!


Solution

  • I think you want to add Files.items(). And then compare the files to the iterator generated from that. 'Files' points to the panda Series.