change file extension

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • foker500
    New Member
    • Jun 2006
    • 4

    change file extension

    I'm a newbie so please excuse my ingnorance. I have serveral hunderd files in a folder and need to change the file extension from '.pnt' to '.txt'. Any suggestions?

    Thanks
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by foker500
    I'm a newbie so please excuse my ingnorance. I have serveral hunderd files in a folder and need to change the file extension from '.pnt' to '.txt'. Any suggestions?

    Thanks

    Ok, I'm not exactly sure about this but I think you have to use the 'os' module somehow like this:
    Code:
    files = os.listdir(os.curdir)
    for file in files:
        if '.pnt' in file:
            newfile = file.replace('.pnt', '.txt')
            os.rename(file, newfile)
    not exactly sure though.

    Comment

    • ghostdog74
      Recognized Expert Contributor
      • Apr 2006
      • 511

      #3
      another way
      Code:
      import os,glob
      os.chdir(dir)
      for fi in glob.glob("*.pnt"):
         os.rename(fi, fi[:-3] + "txt")

      Comment

      • chikatambun
        New Member
        • Apr 2015
        • 1

        #4
        Code:
        os.chdir(value)
        for aFile in glob.glob("*.php"):
            base = os.path.splitext(aFile)[0]
        os.rename(aFile, base + ".null")
        Last edited by chikatambun; Apr 13 '15, 09:13 PM. Reason: file python built in function rename

        Comment

        • dwblas
          Recognized Expert Contributor
          • May 2008
          • 626

          #5
          Note that you should not use "file" as a variable name. It over-writes Python's built in function with the same name https://docs.python.org/2/library/functions.html#file

          Comment

          Working...