Search code examples
pythonpathlib

Using pathlib with dataframe


I have a dataframe with file paths and file names

I am trying to use this code to iterate through the dataframe and print the file name

the following works when I test the actual pathname and filename that is in the dataframe columns

from pathlib import Path
for index, row in df.iterrows():
    for path in Path('Z:\\A\\2021-07-21\\DL\\').rglob('xx_240721_21*_AT_B.txt.bz2'):
        print(path) 

When i try and parse the actual values using the dataframe values, it does not work

from pathlib import Path
for index, row in df.iterrows():
    search = df['Search2']
    filename = df['filename']
    for path in Path('Search').rglob('filename'):
        print(path) 

I can confirm that the Search2 and filename have the exact strings as what I was testing with

Still new to python so any help will be helpful.As always, I have tried to do some research on this but got stuck


Solution

  • I was able to figure this out and used the following

    from pathlib import Path
    for index, row in df.iterrows():
        search = (row['Search2'])
        filename= (row['filename'])
        for path in Path(search).rglob(filename):
            print(path.name)