Jul-29-2020, 03:24 PM
(This post was last modified: Jul-29-2020, 03:36 PM by Kristenl2784.)
(Jul-29-2020, 02:50 PM)deanhystad Wrote: To debug a problem like this the first thing I would do is verify I am getting a list of files.
input_dir = 'C:/Users/work/comparison' for file in os.listdir(input_dir): print(file, os.path.isfile(file), file.endswith(".xlsx"))Well, that isn't true. The first thing I would do is replace os with pathlib
from pathlib import Path input_dir = Path('C:/Users/work/comparison') for file in input_dir.iterdir(): print(file, Path.isfile(file), file.endswith(".xlsx"))Either way the first thing is make sure I am getting a list of files. If that doesn't work nothing else is going to work. If I am getting files I want to verify that my test for spreadsheet files is going to work.
Usually I don't wait for errors to occur to do debugging. If I am unsure about anything I usually write it as test. With your code I probably would have started like this:
import openpyxl as xl import os import pandas as pd import xlsxwriter input_dir = 'C:/Users/work/comparison' files = [] for file in os.listdir(input_dir): input_file = os.path.join(input_dir, file) print(file, input_file, os.path.isfile(file), file.endswith(".xlsx")) for file in files: input_file = os.path.join(input_dir, file) wb1=xl.load_workbook(input_file) ws1=wb1.worksheets[0]When I was confident about finding spreadsheet files in the directory I would then remove the test and replace with the list comprehension. When I run this on my computer (with a different input_dir) it was pretty obvious why "files" in your code is an empty list.
I don't think I follow what you're doing or trying to explain. Also, my code works fine if the .xlsx files are in with the code, one directory back. But when I place the .xlsx files one directory up into their own folder, and update the input_dir path it doesn't find any files.
This worked:
files = [file for file in os.listdir(input_dir)
if os.path.isfile(os.path.join(input_dir, file)) and file.endswith(".xlsx")]