Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.list dir not working
#5
(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")]
Reply


Messages In This Thread
os.list dir not working - by Kristenl2784 - Jul-29-2020, 01:41 PM
RE: os.list dir not working - by Axel_Erfurt - Jul-29-2020, 02:00 PM
RE: os.list dir not working - by Kristenl2784 - Jul-29-2020, 02:14 PM
RE: os.list dir not working - by deanhystad - Jul-29-2020, 02:50 PM
RE: os.list dir not working - by Kristenl2784 - Jul-29-2020, 03:24 PM
RE: os.list dir not working - by Marbelous - Jul-29-2020, 03:59 PM
RE: os.list dir not working - by deanhystad - Jul-29-2020, 04:12 PM
RE: os.list dir not working - by Marbelous - Jul-29-2020, 04:20 PM
RE: os.list dir not working - by deanhystad - Jul-29-2020, 04:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python3: iterating through list not working wardancer84 3 2,382 Jul-08-2020, 04:30 PM
Last Post: DPaul
  Finding MINIMUM number in a random list is not working Mona 5 3,100 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Appending to list not working and causing a infinite loop eiger23 8 4,036 Oct-10-2019, 03:41 PM
Last Post: eiger23
  list not working Zman350x 2 2,406 Mar-10-2018, 12:21 AM
Last Post: Zman350x

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020