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

作者:shichen2014 时间:2021-02-19 20:51:14 

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

1. 获取文件后缀名:

#!/usr/bin/python
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


这里的关键是os.path.splitext()
如abc/ef.g.h ,这里获取到的是h

2. python查找遍历指定文件路径下指定后缀名的文件实例:

import os
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()


3. 批量重命名目录中的文件后缀实例:

import os
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])


例子:将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程序设计有所帮助。

标签:python,文件,后缀
0
投稿

猜你喜欢

  • python中lambda函数 list comprehension 和 zip函数使用指南

    2021-08-28 22:16:21
  • 使用python 获取进程pid号的方法

    2023-06-11 21:25:52
  • Python实现将数据框数据写入mongodb及mysql数据库的方法

    2021-10-07 02:24:18
  • 利用Python多处理库处理3D数据详解

    2021-03-05 18:11:18
  • Pandas的read_csv函数参数分析详解

    2021-06-02 13:40:15
  • Python实现读取csv文件并进行排序

    2021-06-27 08:37:59
  • 详解Python计算机视觉 图像扭曲(仿射扭曲)

    2021-06-13 15:57:43
  • CSS处理斜角导航条的一个例子

    2007-08-27 12:38:00
  • mysql慢查询的分析方法

    2010-08-03 14:51:00
  • 让数据站住脚-浅谈用户研究中的信度与效度

    2010-09-10 13:14:00
  • 巧用Dreamweaver MX设计导航栏特效

    2009-07-10 13:17:00
  • 驯服不听话的网页表格

    2007-12-03 11:36:00
  • Oracle 函数大全

    2009-07-23 14:29:00
  • Python二进制数据结构Struct的具体使用

    2022-07-10 00:01:59
  • TensorFlow的环境配置与安装教程详解(win10+GeForce GTX1060+CUDA 9.0+cuDNN7.3+tensorflow-gpu 1.12.0+python3.5.5)

    2022-11-08 00:54:03
  • python中return如何写

    2023-11-17 21:44:56
  • FrontPage XP中的设计技巧

    2008-07-17 10:49:00
  • Python 树表查找(二叉排序树、平衡二叉树)

    2021-01-27 03:06:20
  • 利用Pycharm + Django搭建一个简单Python Web项目的步骤

    2021-10-21 15:35:47
  • 如何用SA-FileUp上传一个单纯的HTML文件?

    2010-05-18 18:29:00
  • asp之家 网络编程 m.aspxhome.com