I’m stuck on 2 things.
First thing - if a file cant be found use “None” as the the file path.
It currently just skips over it. I tried using “if exist” thing but it keeps returning true for the one that doesn’t exist so I couldn’t get a else/if statement to work.
Second thing-I need the file path to be appended into the same csv its reading from( or make a new csv keeping the old data in the other cells). The row that i want to append into I labeled “Pathname”. When i tried doing this i could get it to go into the same column but not the same row as the “parent_part_no” it used to search. For example search for the file in column 1/row 1 then enter the file path into column 2/row 1 if no file path found enter none. Also if a file path already exist don’t append the existing data.
The code i have below is what works for getting the file path. I’m using pandas for the “usecols” parameter because i can’t always guarantee the columns being in the same order/location. (they change stuff with out telling me)
import pandas as pd import os Path = r"P:/Production/Product/Print Ready Files" Row = 0 df = pd.read_csv("File_Path_Finder/Filenamelist.csv", header=0, usecols=[ "parent_part_no", "parent_art_path"]) while Row < len(df): Parentname = ((df.loc[Row, ('parent_part_no')])+".jpg") Pathname = (df.loc[Row, ('parent_art_path')]) ##not being used currently for root, dirs, files in os.walk(Path): for file in files: if file.endswith(Parentname): Filepath = (root+'/' + file) print(Filepath) Row += 1
Csv looks like this for testing. The real .csv that will be used has 47 columns so I trimmed it down to the two I’m going to be using.
parent_part_no,parent_art_path 18079 - Gather EXTRA 15.75x44.75 16.625x45.625, 160109 - I Have Found The One My Soul Loves EXTRA 12x16 13x17, 18079 - Gather EXTRA 15.75x44.75 16.625x45.6252, Dont exist - anywhere, 194459 - Be The Light EXTRA 2.75x5.254,
It prints this…
P:/Production/Product/Print Ready Files\IRN Extra Art/18079 - Gather EXTRA 15.75x44.75 16.625x45.625.jpg P:/Production/Product/Print Ready Files\IRN Extra Art/160109 - I Have Found The One My Soul Loves EXTRA 12x16 13x17.jpg P:/Production/Product/Print Ready Files\IRN Extra Art/18079 - Gather EXTRA 15.75x44.75 16.625x45.6252.jpg P:/Production/Product/Print Ready Files\IRN Extra Art/194459 - Be The Light EXTRA 2.75x5.254.jpg
Any help would be great!