10

how can I list only file names in a directory without directory info in the result? I tried

for file in glob.glob(dir+filetype): print file 

give me result /path_name/1.log,/path_name/2.log,.... but what I do need is file name only: 1.log, 2.log, etc. I do not need the directory info in the result. is there a simple way to get rid of the path info? I don't want to some substr on the result. Thank you!

3
  • 3
    dir is a built-in function, dir(). You should choose another variable name in case any of your modules use the function, as it could lead to confusing bugs. Commented Apr 18, 2013 at 18:55
  • thank you timss for pointing out this Commented Apr 18, 2013 at 19:12
  • And likewise, as @timss points out in a comment to his own answer, you shouldn't use file either, as it's the name of a builtin type (and therefore also callable as a function, which works just like open). Commented Apr 18, 2013 at 19:22

3 Answers 3

31

os.path.basename:

Return the base name of pathname path. This is the second element of the pair returned by passing path to the function split(). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').

So:

>>> os.path.basename('/path_name/1.log,/path_name/2.log') '2.log' 
Sign up to request clarification or add additional context in comments.

4 Comments

thank you. I applied os.path.basename on the result like this: for file in glob.glob(adir+filetype): print os.path.basename (file) I get what I expected.
Not to sound annoyed or anything, but I genuinely wonder why this is the most popular answer. How is glob.glob and os.path.basename better than the simple os.listdir? os.listdir strikes me as more flexible, especially since it'll give you a list right away, which is probably what you want in most cases. Maybe most people don't like list comprehension?
@timss: The OP specifically wants to get only the files that match a glob-style wildcard expression. Sure, he could use listdir plus fnmatch, or rethink his code (or possibly even his user interface) so he has some other rule instead of a glob-style wildcard expression… but why? That's exactly what glob is for.
@abarnert Aye, thanks for answering my silly question. I somehow forgot to consider the posibility that OP might use a wildcard expression, and not a simple path.
3
import os # Do not use 'dir' as a variable name, as it's a built-in function directory = "path" filetype = "*.log" # ['foo.log', 'bar.log'] [f for f in os.listdir(directory) if f.endswith(filetype[1:])] 

10 Comments

Presumably he's got a string filetype="*.log", so you need to do file.endswith(filetype[1:]) or something… and hope that's the only kinds of glob pattern you ever get. (Also, you have to spell the methods right.)
Of course, but OP doesn't say anything about the use of a filetype variable being final, just how to list all files in a directory with a given filetype without full path. Thanks for pointing out the spelling error.
Well, you have to assume that his code represents what he's trying to do, especially given that his code apparently already works, other than returning paths instead of just filenames.
Fair point. I updated my answer (and changed file with f to not overwrite class file(object)
Well, the help is written separately from the code. In CPython 2.x, a file is a wrapper around a PyFile * (as defined here). There's no actual inheritance… but since object has no non-default method slots, the help system adds the class foo(object) for any builtin type that doesn't say otherwise, and isinstance and subclass treat any type as if it were a subclass of object, I guess it is correct as far as any reasonable attempt to detect it from within Python. Is that good enough?
|
1

you can do

import os [file.rsplit(os.path.sep, 1)[1] for file in glob.glob(dir+filetype)] 

4 Comments

Don't you want [-1] instead?
Sure, but is there a difference ? Just curious
I take it back, didn't notice it was rsplit which I'm not familiar with.
The whole reason for os.path is that you shouldn't try to do pathname manipulation in terms of string manipulation, because it's harder to read, easier to get wrong, and more likely to confuse other readers (as happened with Mark Ransom). Also, this is presumably the kind of thing the OP meant by "I don't want to some substr on the result".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.