finding files that have extensions
Fredrik Lundh
fredrik at pythonware.com
Sun Dec 7 15:03:22 EST 2003
"hokiegal99" wrote:
> The problem I'm having is that as I add more and more file types to
> the script I have to define more and more 'xxx_skip' variables. My
> 'if' conditionals are growing larger and larger. Which leads to my
> question: How could I define it so that *any* file that already has a
> '.xxx' extension (where x = 'abcdefghijklmnopqrstuvwxyz' upper and
> lowercase) would be excluded from the rename? I've considered
> something like this:
>
> ext = re.compile('[abcdefghijklmnopqrstuvwxyz]', re.IGNORECASE)
>
> But I don't know how to pull 'ext' into something like this:
>
> ext_skip = string.find(fname,'.xxx')
>
> Any ideas?
os.path.splitext(fname) splits a filename into prefix and extension
parts:
>>> os.path.splitext("hello")
('hello', '')
>>> os.path.splitext("hello.doc")
('hello', '.doc')
>>> os.path.splitext("hello.DOC")
('hello', '.DOC')
>>> os.path.splitext("hello.foo")
('hello', '.foo')
in other words, "if not os.path.splitext(fname)[1]" might be what
you want. or more readable:
for fname in files:
name, ext = os.path.splitext(fname)
if ext:
continue
# ... figure out the new name and rename it ...
</F>
More information about the Python-list
mailing list