Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.list dir not working
#9
I also think there are times to use list comprehension and times when it is better to use a loop. This is ugly:
files = [file for file in os.listdir(input_dir)
         if os.path.isfile(file) and file.endswith(".xlsx")]
I know it is ugly because later on you used os.path.join so you knew it was needed. The only reason you didn't use it in the list comprehension is that the ungainly syntax hid the need.

This is longer, but it is easier to read:
files = []
for file in os.listdir(input_dir)
    file = os.path.join(input_dir, file)
    if os.path.isfile(file) and file.endswith(".xlsx"):
        files.append(file)
And when you are done with the loop you have a list of filenames with paths.
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,368 Jul-08-2020, 04:30 PM
Last Post: DPaul
  Finding MINIMUM number in a random list is not working Mona 5 3,057 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Appending to list not working and causing a infinite loop eiger23 8 4,009 Oct-10-2019, 03:41 PM
Last Post: eiger23
  list not working Zman350x 2 2,385 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