-3

How do I change the name of some PDF file (*.pdf) to convert.pdf?

There is only one PDF file in the relevant directory.

thegrinner
  • 11,546
  • 5
  • 41
  • 64
KoenP
  • 25
  • 5

1 Answers1

3

It seems very silly to do this in Python when you could just write mv *.pdf convert.pdf in an sh-compatible shell (the default on most non-Windows systems) or rename *.pdf convert.pdf in cmd (the default on Windows). But if you really want to, you can do it in two ways.

import glob, os
for name in glob.iglob('*.pdf'):
    os.rename(name, 'convert.pdf')

import os
for name in os.listdir('.'):
    if os.path.splitext(name)[1] == '.pdf':
        os.rename(name, 'convert.pdf')
abarnert
  • 354,177
  • 51
  • 601
  • 671