python中操作文件的模块的方法总结


Posted in Python onFebruary 04, 2021

在python中操作文件算是一个基本操作,但是选对了模块会让我们的效率大大提升。本篇整理了两种模块的常用方法,分别是os模块和shutil模块。相信这两种模块大家在之间的学习中有所涉及,那么关于具体的文件操作部分,我们一起往下看看都有哪些方法和实例吧。

本教程操作环境:windows7系统、Python3版、Dell G3电脑。

Python对文件操作采用的统一步骤是:打开—操作—关闭。

一、python中对文件、文件夹操作时经常用到的os模块和shutil模块常用方法

1.得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd()

2.返回指定目录下的所有文件和目录名:os.listdir()

3.函数用来删除一个文件:os.remove()

4.删除多个目录:os.removedirs(r"c:\python")

5.检验给出的路径是否是一个文件:os.path.isfile()

6.检验给出的路径是否是一个目录:os.path.isdir()

7.判断是否是绝对路径:os.path.isabs()

8.检验给出的路径是否真地存:os.path.exists()

9.返回一个路径的目录名和文件名:os.path.split()

二、文件综合操作实例

将文件夹下所有图片名称加上'_fc'

# -*- coding:utf-8 -*-
import re
import os
import time
#str.split(string)分割字符串
#'连接符'.join(list) 将列表组成字符串
def change_name(path):
global i
if not os.path.isdir(path) and not os.path.isfile(path):
return False
if os.path.isfile(path):
file_path = os.path.split(path) #分割出目录与文件
lists = file_path[1].split('.') #分割出文件与文件扩展名
file_ext = lists[-1] #取出后缀名(列表切片操作)
img_ext = ['bmp','jpeg','gif','psd','png','jpg']
if file_ext in img_ext:
os.rename(path,file_path[0]+'/'+lists[0]+'_fc.'+file_ext)
i+=1 #注意这里的i是一个陷阱
#或者
#img_ext = 'bmp|jpeg|gif|psd|png|jpg'
#if file_ext in img_ext:
# print('ok---'+file_ext)
elif os.path.isdir(path):
for x in os.listdir(path):
change_name(os.path.join(path,x)) #os.path.join()在路径处理上很有用
img_dir = 'D:\\xx\\xx\\images'
img_dir = img_dir.replace('\\','/')
start = time.time()
i = 0
change_name(img_dir)
c = time.time() - start
print('程序运行耗时:%0.2f'%(c))
print('总共处理了 %s 张图片'%(i))

实例扩展:

#! python 3
# -*- coding:utf-8 -*-
# Autor: GrayMac
import shutil
import os

basefileclass = 'basefile'
#sourcefile:源文件路径 fileclass:源文件夹 destinationfile:目标文件夹路径
def copy_file(sourcefile,fileclass,destinationfile):
 #遍历目录和子目录
 for filenames in os.listdir(sourcefile):
  #取得文件或文件名的绝对路径
  filepath = os.path.join(sourcefile,filenames)
  #判断是否为文件夹
  if os.path.isdir(filepath):
   if fileclass == basefileclass :
    copy_file(filepath,fileclass + '/' + filenames,destinationfile + '/' + filenames)
   else :
    copy_file(filepath,fileclass,destinationfile + '/' + filenames)
  #判断是否为文件
  elif os.path.isfile(filepath):
   print('Copy %s'% filepath +' To ' + destinationfile)
   #如果无文件夹则重新创建
   if not os.path.exists(destinationfile):
    os.makedirs(destinationfile)
   shutil.copy(filepath,destinationfile)
    
copy_file(sourcefile,basefileclass,destinationfile)

到此这篇关于python中操作文件的模块的方法总结的文章就介绍到这了,更多相关python中操作文件的模块有几种内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
python中关于日期时间处理的问答集锦
Mar 08 Python
介绍Python中的fabs()方法的使用
May 14 Python
wxPython的安装图文教程(Windows)
Dec 28 Python
EM算法的python实现的方法步骤
Jan 02 Python
详解python--模拟轮盘抽奖游戏
Apr 12 Python
python机器学习包mlxtend的安装和配置详解
Aug 21 Python
python 定义类时,实现内部方法的互相调用
Dec 25 Python
3分钟看懂Python后端必须知道的Django的信号机制
Jul 26 Python
pytorch学习教程之自定义数据集
Nov 10 Python
Python中tkinter的用户登录管理的实现
Apr 22 Python
python3+PyQt5+Qt Designer实现界面可视化
Jun 10 Python
Python使用BeautifulSoup4修改网页内容
May 20 Python
Python3利用openpyxl读写Excel文件的方法实例
Feb 03 #Python
python之openpyxl模块的安装和基本用法(excel管理)
Feb 03 #Python
python中time.ctime()实例用法
Feb 03 #Python
python中Array和DataFrame相互转换的实例讲解
Feb 03 #Python
利用Python过滤相似文本的简单方法示例
Feb 03 #Python
python time.strptime格式化实例详解
Feb 03 #Python
Python字符串的15个基本操作(小结)
Feb 03 #Python
You might like
php中的注释、变量、数组、常量、函数应用介绍
2012/11/16 PHP
CI框架常用经典操作类总结(路由,伪静态,分页,session,验证码等)
2016/11/21 PHP
javascript 多级checkbox选择效果
2009/08/20 Javascript
基于jQuery的树控件实现代码(asp.net+json)
2010/07/11 Javascript
JavaScript中的eval()函数详解
2013/08/22 Javascript
JS与jQuery遍历Table所有单元格内容的方法
2015/12/07 Javascript
JavaScript操作HTML DOM节点的基础教程
2016/03/11 Javascript
简单理解vue中Props属性
2016/10/27 Javascript
Bootstrap CSS使用方法
2016/12/23 Javascript
Angular.js 4.x中表单Template-Driven Forms详解
2017/04/25 Javascript
webpack配置sass模块的加载的方法
2017/07/30 Javascript
vue+iview写个弹框的示例代码
2017/12/05 Javascript
微信小程序实现流程进度的图样式功能
2018/01/16 Javascript
JavaScript面向对象继承原理与实现方法分析
2018/08/09 Javascript
解决angularjs中同步执行http请求的方法
2018/08/13 Javascript
微信小程序实现两个页面传值的方法分析
2018/12/11 Javascript
VScode格式化ESlint方法(最全最好用方法)
2019/09/10 Javascript
[06:09]辉夜杯主赛事开幕式
2015/12/25 DOTA
python中使用urllib2伪造HTTP报头的2个方法
2014/07/07 Python
分析Python中设计模式之Decorator装饰器模式的要点
2016/03/02 Python
Python 查看文件的编码格式方法
2017/12/21 Python
python合并同类型excel表格的方法
2018/04/01 Python
Python使用logging模块实现打印log到指定文件的方法
2018/09/05 Python
python分批定量读取文件内容,输出到不同文件中的方法
2018/12/08 Python
对pytorch中的梯度更新方法详解
2019/08/20 Python
Python模块汇总(常用第三方库)
2019/10/07 Python
python面向对象之类属性和类方法案例分析
2019/12/30 Python
Python ORM框架Peewee用法详解
2020/04/29 Python
css3教程之倾斜页面
2014/01/27 HTML / CSS
北卡罗来纳州豪华家具和家居装饰店:Carolina Rustica
2018/10/30 全球购物
在阿联酋购买翻新手机和平板电脑:Teckzu
2021/02/12 全球购物
餐饮业创业计划书范文
2014/01/06 职场文书
村支部书记群众路线对照检查材料思想汇报
2014/10/08 职场文书
2014年银行工作总结范文
2014/11/12 职场文书
初三学生语文考试作弊检讨书
2014/12/14 职场文书
2015公司年度工作总结
2015/05/14 职场文书