finding files that have extensions

Levente Sandor sandorlevi at yahoo.com
Sun Dec 7 19:58:16 EST 2003


Have a look at the glob and fnmatch modules, they should help.
----
levi

hokiegal99 at hotmail.com (hokiegal99) wrote in message news:<93f5c5e9.0312071154.4e7a1311 at posting.google.com>...
> Hi,
> 
> I have a working Python script that renames files that don't currently
> have PC based file extensions. For example, if there is a MS Word file
> that does not have '.doc' on the end of it, the script will append
> that. The script also knows to *not* add an extension if the file
> already has one. Bascially, here's how I'm doing it:
> 
>    for fname in files: 
>       doc_id = string.find(file(os.path.join(root,fname),
> 'rb').read(), 'Word.Document.')
>       doc_skip = string.find(fname,'.doc')
>       DOC_skip = string.find(fname,'.DOC')
>       if doc_id >=1 and doc_skip ==-1 and DOC_skip ==-1:
>          newpath = os.path.join(root,doc_new)
>          oldpath = os.path.join(root,fname)
>          os.renames(oldpath,newpath)
> 
> 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?
> 
> TIA!!!




More information about the Python-list mailing list