• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python获取文件后缀名及批量更新目录下文件后缀名的方法

python获取文件后缀名及批量更新目录下文件后缀名的方法

作者:shichen2014 字体:[增加 减小] 来源:互联网

shichen2014 通过本文主要向大家介绍了python 文件后缀名,python后缀名,python 获取文件后缀,python 判断文件后缀,python脚本的后缀等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了python获取文件后缀名及批量更新目录下文件后缀名的方法。分享给大家供大家参考。具体实现方法如下:

1. 获取文件后缀名:
import os
dict = {}
for d, fd, fl in os.walk('/home/ahda/Program/'):
        for f in fl:
                sufix = os.path.splitext(f)[1][1:]
                if dict.has_key(sufix):
                        dict[sufix] += 1
                else:
                        dict[sufix] = 1
for item in dict.items():
        print "%s : %s" % item</div>
这里的关键是os.path.splitext()
如abc/ef.g.h ,这里获取到的是h

2. python查找遍历指定文件路径下指定后缀名的文件实例:
import sys
import os.path
for dirpath, dirnames, filenames in os.walk(startdir):
        for filename in filenames:
            if os.path.splitext(filename)[1] == '.txt':
               filepath = os.path.join(dirpath, filename)
               #print("file:" + filepath)
               input_file = open(filepath)
               text = input_file.read()
               input_file.close()
              
               output_file = open( filepath, 'w')
               output_file.write(text)
               output_file.close()</div>
3. 批量重命名目录中的文件后缀实例:
def swap_extensions(dir, before, after):
    if before[:1] != '.': #如果参数中的后缀名没有'.'则加上
        before = '.' + before
    thelen = -len(before)
    if after[:1] != '.':
        after = '.' + after
    for path, subdir, files in os.walk(dir):
        for oldfile in files:
            if oldfile[thelen:] == before:
                oldfile = os.path.join(path, oldfile)
                newfile = oldfile[:thelen] + after
                os.rename(oldfile, newfile)
                print oldfile +' changed to ' + newfile
if __name__ == '__main__':
    import sys
    if len(sys.argv) != 4:
        print 'Usage:swap_extension.py rootdir before after'
        sys.exit(1)
    swap_extensions(sys.argv[1], sys.argv[2], sys.argv[3])</div>
例子:将e:/py/test目录下.php结尾的文件重命名为.py
 
E:py>python_cook e:/py/test .php .py
e:/py/testtest.php changed to e:/py/testtest.py
e:/py/test1.php changed to e:/py/test1.py
e:/py/test2.php changed to e:/py/test2.py

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • python获取文件后缀名及批量更新目录下文件后缀名的方法
  • Python批量修改文件后缀的方法

相关文章

  • 详解MySQL数据类型int(M)中M的含义
  • 处理Python中的URLError异常的方法
  • Python的函数嵌套的使用方法
  • Python中optparse模块使用浅析
  • python中enumerate函数遍历元素用法分析
  • 解读Django框架中的低层次缓存API
  • Python isinstance判断对象类型
  • python访问纯真IP数据库的代码
  • Python获取文件所在目录和文件名的方法
  • 用Python抢过年的火车票附源码

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 用Python从零实现贝叶斯分类器的机器学习的教程
    • python基于隐马尔可夫模型实现中文拼音输入
    • Python常用的爬虫技巧总结
    • Python 递归函数详解及实例
    • 分享15个最受欢迎的Python开源框架
    • 在Python中使用列表生成式的教程
    • python类定义的讲解
    • python通过cookie模拟已登录状态的初步研究
    • Python实现发送email的几种常用方法
    • 在Python中操作时间之mktime()方法的使用教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有