469,294 Members | 1,662 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,294 developers. It's quick & easy.

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
Mar 6 '07 #1
4 30053
ilikepython
844 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. files = os.listdir(os.curdir)
  2. for file in files:
  3.     if '.pnt' in file:
  4.         newfile = file.replace('.pnt', '.txt')
  5.         os.rename(file, newfile)
  6.  
not exactly sure though.
Mar 6 '07 #2
ghostdog74
511 Expert 256MB
another way
Expand|Select|Wrap|Line Numbers
  1. import os,glob
  2. os.chdir(dir)
  3. for fi in glob.glob("*.pnt"):
  4.    os.rename(fi, fi[:-3] + "txt")     
  5.  
Mar 7 '07 #3
Expand|Select|Wrap|Line Numbers
  1. os.chdir(value)
  2. for aFile in glob.glob("*.php"):
  3.     base = os.path.splitext(aFile)[0]
  4. os.rename(aFile, base + ".null")
  5.  
Apr 11 '15 #4
dwblas
626 Expert 512MB
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
Apr 13 '15 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by b.milbrandt | last post: by
3 posts views Thread by Shapper | last post: by
1 post views Thread by Nicky | last post: by
11 posts views Thread by cybervigilante | last post: by
3 posts views Thread by adamjblakey | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.