更改文件夹中文件的文件扩展名? [英] Change the file extension for files in a folder?

查看:63
本文介绍了更改文件夹中文件的文件扩展名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改特定文件夹中文件的扩展名.我在论坛上阅读了有关此主题的信息.使用确实"的想法,我编写了以下代码,我希望它会工作,但不会.我很感谢您找到我的错误的任何指导.

I would like to change the extension of the files in specific folder. i read about this topic in the forum. using does ideas, I have written following code and I expect that it would work but it does not. I would be thankful for any guidance to find my mistake.

   import os,sys
   folder = 'E:/.../1936342-G/test'
   for filename in os.listdir(folder):
           infilename = os.path.join(folder,filename)
           if not os.path.isfile(infilename): continue
           oldbase = os.path.splitext(filename)
           infile= open(infilename, 'r')
           newname = infilename.replace('.grf', '.las')
           output = os.rename(infilename, newname)
           outfile = open(output,'w')

推荐答案

源文件上的open是不必要的,因为os.rename仅需要源路径和目标路径即可完成工作.而且,os.rename总是返回None,因此在其返回值上调用open没有任何意义.

The open on the source file is unnecessary, since os.rename only needs the source and destination paths to get the job done. Moreover, os.rename always returns None, so it doesn't make sense to call open on its return value.

import os,sys
folder = 'E:/.../1936342-G/test'
for filename in os.listdir(folder):
       infilename = os.path.join(folder,filename)
       if not os.path.isfile(infilename): continue
       oldbase = os.path.splitext(filename)
       newname = infilename.replace('.grf', '.las')
       output = os.rename(infilename, newname)

我只是删除了两个open.检查它是否适合您.

I simply removed the two open. Check if this works for you.

这篇关于更改文件夹中文件的文件扩展名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆