finding files that have extensions

H

hokiegal99

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!!!
 
F

Fredrik Lundh

hokiegal99 said:
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:
('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>
 
J

James Kew

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?

Take a look at os.path.splitext -- testing os.path.splitext(filename)[1]
will tell you if the filename carries an extension or not.

James
 
H

hokiegal99

James Kew said:
Take a look at os.path.splitext -- testing os.path.splitext(filename)[1]
will tell you if the filename carries an extension or not.

That's a great tip!!! This bit of code does it:

ext = os.path.splitext(fname)
if not ext[1]:
do the rename...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top