How do I see if the contents of a csv file exists as a file in another directory?

Question:

EDIT:
To better explain my dilemma, I have a csv file that lists a number of applications numbered XXXXXX. Each of these applications have a corresponding xml file that exists in another directory. I’m essentially attempting to write a script that.

  1. unzips the folder that contains the xml files and the csv file.
  2. parse the entries within the csv file and sees that that each application listed in the csv file has a corresponding xml file.
  3. Output another CSV file that sets an application to true if the xml file exists.

So far I’ve written the script to unzip, but I’m having a hard time wrapping my head around step 2 and 3.

from tkinter import Tk
from tkinter.filedialog import askdirectory
import zipfile
import os
import xml.etree.ElementTree as ET
import pandas as pd
from datetime import datetime


def unzipXML(root):
    
    print(f'({datetime.now().strftime("%b. %d - %H:%M:%S")}) Stage 1 of 5: Unzipping folder(s)...')
    
    # Get filepaths of .zip files
    zipPaths = []
    for filename in os.listdir(root):
        if filename.endswith(".zip"):
            zipPaths.append(root + "/" + filename)

    # Unzip all .zip files
    for path in zipPaths:
        with zipfile.ZipFile(path, 'r') as zipRef:
            zipRef.extractall(root)

    print(f'({datetime.now().strftime("%b. %d - %H:%M:%S")}) {len(zipPaths)} folder(s) unzipped successfully.')
Asked By: Koomos

||

Answers:

I don’t know if I understand your problem, but maybe this will help:

#Get files from path
List_Of_Files = glob.glob('./' + '\*.csv')

for file_name in List_Of_Files:
    if file_name == your_var:
        ...
Answered By: BoTop

Loop through the names in the csv, calling os.path.exists() on each one.

with open("filenames.csv") as inf, open("apps.csv", "w") as outf:
    in_csv = csv.reader(inf)
    out_csv = csv.writer(outf)
    for row in in_csv:
        app_name = row[0] # replace [0] with the correct field number for your CSV
        if os.path.exists(os.path.join(directory_path, app_name + ".xml")):
            out_csv.writerow([app_name, 'exists'])
        else:
            out_csv.writerow([app_name, 'notexists'])
Answered By: Barmar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.