finding files that have extensions

Mel Wilson mwilson at the-wire.com
Sun Dec 7 15:13:13 EST 2003


In article <93f5c5e9.0312071154.4e7a1311 at posting.google.com>,
hokiegal99 at hotmail.com (hokiegal99) wrote:
>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')

   I'm not sure I grasp all the ramifications, but maybe you
can parameterize the whole thing:

    characteristics = [
            ('Word Document.', ['.doc', '.dot', '.txt', '.wps'])
        ]

and so on, with 'magic' identifying strings and proper
extensions.

Then

    name, ext = os.path.splitext (fname)
    for magic, extensions in characteristics:
        if ( string.find(file(os.path.join(root,fname),>'rb').read()
                , magic) >= 1 ):
            if not ext.lower() in extensions:
                newpath = os.path.join (root, name + extensions[0])
>               oldpath = os.path.join(root,fname)
>               os.renames(oldpath,newpath)
            break

Or something.

        Regards.        Mel.




More information about the Python-list mailing list