Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
build array list
#1
I am writing function to return the list of files in unix directory. it is returning one file.
fl = []
WORKING_DIR='../'+sys.argv[1]+'/'
def find_case_insensitve(dirname, extensions):
WORKING_DIR='../'+sys.argv[1]+'/'
for filename in glob.glob(dirname):
base, ext = os.path.splitext(filename)
if ext.lower() in extensions:
fl.append(filename)
return list(fl)
print(fl)


fln=find_case_insensitve(WORKING_DIR+"*", ['.sql'])
print(fln)
Reply
#2
Post your code again in Python code tags, so it will be readable. You can find help here.
Reply
#3
fl = []
WORKING_DIR='../'+sys.argv[1]+'/'
def find_case_insensitve(dirname, extensions):
WORKING_DIR='../'+sys.argv[1]+'/'
for filename in glob.glob(dirname):
base, ext = os.path.splitext(filename)
if ext.lower() in extensions:
fl.append(filename)
return list(fl)
print(fl)


fln=find_case_insensitve(WORKING_DIR+"*", ['.sql'])
print(fln)
Reply
#4
Please, fix the indentation!
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Jan-13-2018, 08:34 PM)wavic Wrote: Please, fix the indentation!

Quickest way to do this is to use ctrl+shift+v when copy-pasting code.
Reply
#6
Here is how I would do with pathlib
from pathlib import Path

def find_case_insensitive(dirname, extensions):
    sufset = set(x.lower() for x in extensions)
    return [f for f in Path(dirname).iterdir()
            if f.suffix.lower() in sufset]

if __name__ == '__main__':
    working_dir = Path('..')/'2018-01'
    print(working_dir)
    print(find_case_insensitive(working_dir, ['.py', '.csv']))
Actually, it would be more python3ish to return an iterable from find_case_insensitive()
instead of a list.
Reply
#7
fl = []
fln = []
WORKING_DIR='../'+sys.argv[1]+'/'
def find_case_insensitve(dirname, extensions):
    WORKING_DIR='../'+sys.argv[1]+'/'
    for filename in glob.glob(dirname):
        base, ext = os.path.splitext(filename)
        if ext.lower() in extensions:
           fl.append(filename)
           #print(filename)
           return list(fl)
btw, I just started coding today in python. I guess I have to go long way
I can print(filename) when I comment return list(fl)
I want to use in the following way

fln=find_case_insensitve(WORKING_DIR+"*", [".sql"])
print(fln)

I will be using fln array in some other function
Reply
#8
If you want to use glob library there is no need to use os.path.splitext to get the extension:

import glob
import sys

extensions = ('mp3', 'avi', 'mp4', 'mkv']

def find_media_files(path, files):
    result = []

    for ext in files:
        pattern = path + '/*.' + ext # this will become /path/*.mp3 to search for.
        found = glob.glob(pattern)
        result.extend(found)
    
    return result

my_collection = find_media_files(sys.argv[1], extensions)

for file_ in my_collection:
    print(file_)
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
Thank you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  functional LEDs in an array or list? // RPi user Doczu 5 1,636 Aug-23-2022, 05:37 PM
Last Post: Yoriz
Question Keyword to build list from list of objects? pfdjhfuys 3 1,580 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Browse and build a list of folder paths using recursion Ultrainstinct_5 8 5,037 Apr-24-2021, 01:41 PM
Last Post: Ultrainstinct_5
  LIST or ARRAY Comparison and change of value nio74maz 0 1,708 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  2d Array adds last element to entire list waiteup 2 2,105 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  trouble with list array Milfredo 2 2,048 Sep-16-2020, 12:07 AM
Last Post: Milfredo
  Finding an element in a 1d list in a 2d array lionrocker221 0 1,837 Jun-27-2020, 04:50 PM
Last Post: lionrocker221
  Make an array of string number in a List polantas 5 3,127 May-27-2020, 07:18 AM
Last Post: buran
  append list to empty array SchroedingersLion 1 2,185 Feb-02-2020, 05:29 PM
Last Post: SchroedingersLion
  How to read a text file into a list or an array musouka 2 2,898 Oct-07-2019, 01:54 PM
Last Post: musouka

Forum Jump:

User Panel Messages

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