On 2015-02-19 22:55, Jason Friedman wrote:
If you're going to call listdir, you probably want to use fnmatch
directly.
fnmatch seems to be silent on non-existent directories:
python -c 'import fnmatch; fnmatch.fnmatch(/no/such/path, *)'
a better test would be glob.glob as fnmatch
If you're going to call listdir, you probably want to use fnmatch directly.
fnmatch seems to be silent on non-existent directories:
python -c 'import fnmatch; fnmatch.fnmatch(/no/such/path, *)'
--
https://mail.python.org/mailman/listinfo/python-list
Jason Friedman wrote:
It's a shame that glob.glob does not take an arbitrary directory as an
optional argument if one does not want to scan the current directory.
It doesn't have to -- you can give it an absolute path:
from glob import glob
glob(/usr/include/std*.h)
On 02/20/2015 12:51 AM, Jason Friedman wrote:
I'd still advise using my_list.sort() rather than sorted(), as you
don't need to retain the original.
Hmm.
Trying to figure out what that looks like.
If I understand correctly, list.sort() returns None.
What would I return to the caller?
On Fri, 20 Feb 2015 02:08:49 +1100, Chris Angelico wrote:
On Fri, Feb 20, 2015 at 1:16 AM, Denis McMahon
denismfmcma...@gmail.com
wrote:
2. no files match the given pattern
Return either None, 0, False or an empty string.
In both cases, it is then a matter for the calling code to catch
On Fri, Feb 20, 2015 at 7:03 AM, Denis McMahon denismfmcma...@gmail.com wrote:
On the one hand, the return type of a function (when it returns, rather
than raising an exception) should be consistent to itself, even if using
a language where types are not declared.
Yes, so I'd advise against
On Wed, 18 Feb 2015 21:44:12 -0700, Jason Friedman wrote:
My question is, what would be a reasonable behavior/result/return value
if:
1. /path/to/dir does not exist or is not readable
Normally I'd say raise an exception. Whether you choose to use an
existing exception (will trying to read
def order_matching_files(a_path, a_glob=*):
Search a path for files whose names match a_glob
and return a list of the full path to such files, in descending
order of modification time. Ignore directories.
previous_dir = os.getcwd()
os.chdir(a_path)
return_list =
On Thu, 19 Feb 2015 22:51:57 -0700, Jason Friedman wrote:
I'd still advise using my_list.sort() rather than sorted(), as you
don't need to retain the original.
Hmm.
Trying to figure out what that looks like.
If I understand correctly, list.sort() returns None.
What would I return to
I have need to search a directory and return the name of the most recent
file matching a given pattern. Given a directory with these files and
timestamps,
q.pattern1.abc Feb 13
r.pattern1.cdf Feb 12
s.pattern1.efg Feb 10
t.pattern2.abc Feb 13
u.pattern2.xyz Feb 14
v.pattern2.efg Feb
On Fri, Feb 20, 2015 at 4:51 PM, Jason Friedman jsf80...@gmail.com wrote:
I'd still advise using my_list.sort() rather than sorted(), as you
don't need to retain the original.
Hmm.
Trying to figure out what that looks like.
If I understand correctly, list.sort() returns None.
What would I
On Fri, 20 Feb 2015 07:11:13 +1100, Chris Angelico wrote:
On Fri, Feb 20, 2015 at 7:03 AM, Denis McMahon denismfmcma...@gmail.com
wrote:
On the one hand, the return type of a function (when it returns,
rather than raising an exception) should be consistent to itself,
even if using a
On Fri, Feb 20, 2015 at 3:54 PM, Dan Sommers d...@tombstonezero.net wrote:
if there are no
values to return, then return an empty collection.
That one makes sense only if you were going to return a collection
anyway, though. If you were going to return a string, returning an
empty list on
I think I came in a little late, and saw 2. no files match the given
pattern, in which case I'm sticking to my story and returning an empty
list.
The original problem was to search a directory and return the name of
the most recent file matching a given pattern. I'd still prefer an
I'd still advise using my_list.sort() rather than sorted(), as you
don't need to retain the original.
Hmm.
Trying to figure out what that looks like.
If I understand correctly, list.sort() returns None.
What would I return to the caller?
If you're going to call listdir, you probably want
Dan Sommers d...@tombstonezero.net writes:
I'd still prefer an exception to None, and we agree on that an empty
string is bad because it's not a non-string and it could be too easily
mistaken for a filename.
Empty string would be bad. Sometimes I like to simulate an option type,
by returning
Chris Angelico ros...@gmail.com writes:
if len(fs) == 0: ... # didn't get a filename
Bikeshedding: That could be written as simply if not fs. :)
Yeah, in that instance you could do that. It's an unsafe practice when
None is used as the no-value marker, since the empty string is a
perfectly
On Fri, Feb 20, 2015 at 4:13 PM, Jason Friedman jsf80...@gmail.com wrote:
os.chdir(a_path)
return_list = [os.path.join(a_path, x) for x in glob.glob(a_glob) if
os.path.isfile(x)]
os.chdir(previous_dir)
return reversed(sorted(return_list, key=os.path.getmtime))
It's a shame
On Fri, 20 Feb 2015 16:16:50 +1100, Chris Angelico wrote:
On Fri, Feb 20, 2015 at 3:54 PM, Dan Sommers d...@tombstonezero.net wrote:
if there are no
values to return, then return an empty collection.
That one makes sense only if you were going to return a collection
anyway, though. If you
On Fri, Feb 20, 2015 at 5:18 PM, Paul Rubin no.email@nospam.invalid wrote:
Empty string would be bad. Sometimes I like to simulate an option type,
by returning the value as a 1-element list if there's a value, otherwise
as an empty list. So you could say
filename = get_filename(...)[0]
On Fri, Feb 20, 2015 at 1:16 AM, Denis McMahon denismfmcma...@gmail.com wrote:
2. no files match the given pattern
Return either None, 0, False or an empty string.
In both cases, it is then a matter for the calling code to catch the
exception or handle the return value appropriately.
I'd
I have need to search a directory and return the name of the most recent
file matching a given pattern. Given a directory with these files and
timestamps,
q.pattern1.abc Feb 13
r.pattern1.cdf Feb 12
s.pattern1.efg Feb 10
t.pattern2.abc Feb 13
u.pattern2.xyz Feb 14
v.pattern2.efg Feb 10
On 02/19/2015 12:10 AM, Chris Angelico wrote:
On Thu, Feb 19, 2015 at 3:44 PM, Jason Friedman jsf80...@gmail.com wrote:
I have need to search a directory and return the name of the most recent
file matching a given pattern. Given a directory with these files and
timestamps,
q.pattern1.abc Feb
On Thu, Feb 19, 2015 at 3:44 PM, Jason Friedman jsf80...@gmail.com wrote:
I have need to search a directory and return the name of the most recent
file matching a given pattern. Given a directory with these files and
timestamps,
q.pattern1.abc Feb 13
r.pattern1.cdf Feb 12
s.pattern1.efg
24 matches
Mail list logo