Newbie: Joining Lists

M

mosscliffe

I have been playing with GLOB and OS.PATH and it all works, but is
there a better way of getting GLOB to recognise, multiple patterns at
one call (ONE).

Also is it possible to join lists, without the horrid concatenation
code I have (TWO).
I tried list.append(glob.glob(pattern)) but it error'd
----------------------- CODE ----------------------
import os
import glob

filenames = []
patterns = ('.\\t*.py', '.\\*.c??', '.\\*.txt') # Can these patterns
for glob processing be specified in the glob call *****ONE****
for pattern in patterns:
filenames = filenames + glob.glob(pattern) # Is there a better
way for this line in joining lists *****TWO****

for fullfilename in filenames:
(filepath, filename) = os.path.split(fullfilename)
(shortname, extension) = os.path.splitext(filename)
print "PATHNAME: %s FILENAME: %s SHORTNAME: %s EXTNAME: %s" %
(filepath, filename, shortname, extension[1:])
---------------------------------------------------------------------

Thanks

Richard

Ps \\ is because I needed to get the path element for my test and
windoze does not return a path element if in current directory
 
M

Marc 'BlackJack' Rintsch

mosscliffe said:
----------------------- CODE ----------------------
import os
import glob

filenames = []
patterns = ('.\\t*.py', '.\\*.c??', '.\\*.txt') # Can these patterns
for glob processing be specified in the glob call *****ONE****
for pattern in patterns:
filenames = filenames + glob.glob(pattern) # Is there a better
way for this line in joining lists *****TWO****

You can `extend()` the list instead of creating new concatenated copies in
each iteration.
Ps \\ is because I needed to get the path element for my test and
windoze does not return a path element if in current directory

Maybe using `os.path.abspath()` on the file names is a more portable and
robust solution here.

If you don't need all the functionality of `glob.glob()` you can write a
simple function that takes multiple file name patterns:

import fnmatch
import os
import re

def multiglob(patterns):
pattern_re = re.compile('|'.join(map(fnmatch.translate, patterns)))
return [os.path.abspath(path)
for path in os.listdir('.')
if pattern_re.match(path)]

This lacks `glob.glob()`\s special handling of patterns containing
directory names though.

Ciao,
Marc 'BlackJack' Rintsch
 
M

Matimus

One: Not that I know of

Two: You could use list comprehension...
----------------------- CODE ----------------------
import os
import glob

patterns = ('.\\t*.py', '.\\*.c??', '.\\*.txt')
filenames = [glob.glob(pat) for pat in patterns]

# Or as a one liner...

filenames = [glob.glob(pat) for pat in ('.\\t*.py', '.\\*.c??', '.\
\*.txt')]

# Also, I don't think specifying the current directory is necessary.
So this should also work:

filenames = [glob.glob(pat) for pat in ('t*.py', '*.c??', '*.txt')]
 
7

7stud

I have been playing with GLOB and OS.PATH and it all works, but is
there a better way of getting GLOB to recognise, multiple patterns at
one call (ONE).

A better way? You haven't posted a way to do that. And a quick
perusal of the docs for the glob module shows that glob uses some very
simplistic unix like pattern matching, and there are only 4 symbols
you can use. None of them allow alternate patterns, so, no, you can't
match multiple patterns in one pass. You can use os.listdir() and
regexes if you want.
Also is it possible to join lists, without the horrid concatenation
code I have

Horrid? What did you have in mind? You can use the += operator:

filenames += glob.glob(pattern)

If your aversion to concatenation runs too deep, you could use a loop:

for file in glob.glob(pattern):
filenames.append(file)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top