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从sqlite读取并显示数据的方法
May 08 Python
分享Python字符串关键点
Dec 13 Python
Python自然语言处理之词干,词形与最大匹配算法代码详解
Nov 16 Python
基于Python函数的作用域规则和闭包(详解)
Nov 29 Python
python类的方法属性与方法属性的动态绑定代码详解
Dec 27 Python
利用anaconda保证64位和32位的python共存
Mar 09 Python
Django分组聚合查询实例分享
Apr 29 Python
Python爬虫实例——scrapy框架爬取拉勾网招聘信息
Jul 14 Python
Python 删除List元素的三种方法remove、pop、del
Nov 16 Python
python 用opencv实现霍夫线变换
Nov 27 Python
基于Django快速集成Echarts代码示例
Dec 01 Python
OpenCV项目实践之停车场车位实时检测
Apr 11 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生成条形图的方法
2014/12/10 PHP
PHP下SSL加密解密、验证、签名方法(很简单)
2020/06/28 PHP
java微信开发之上传下载多媒体文件
2016/06/24 PHP
JS对img标签进行优化使用onerror显示默认图像
2014/04/24 Javascript
Bootstrap Table使用方法详解
2016/08/01 Javascript
jquery表单插件form使用方法详解
2017/01/20 Javascript
微信小程序 本地数据读取实例
2017/04/27 Javascript
基于VUE选择上传图片并页面显示(图片可删除)
2017/05/25 Javascript
详解使用nvm安装node.js
2017/07/18 Javascript
JavaScript设计模式之单例模式原理与用法实例分析
2018/07/26 Javascript
解决vue-cli webpack打包后加载资源的路径问题
2018/09/25 Javascript
jQuery实现的隔行变色功能【案例】
2019/02/18 jQuery
vue 微信扫码登录(自定义样式)
2020/01/06 Javascript
vue 页面回退mounted函数不执行的解决方案
2020/07/26 Javascript
webstorm建立vue-cli脚手架的傻瓜式教程
2020/09/22 Javascript
python socket网络编程步骤详解(socket套接字使用)
2013/12/06 Python
python操作ie登陆土豆网的方法
2015/05/09 Python
Python使用Srapy框架爬虫模拟登陆并抓取知乎内容
2016/07/02 Python
Python基于PyGraphics包实现图片截取功能的方法
2017/12/21 Python
python调用java的jar包方法
2018/12/15 Python
Python实现繁体中文与简体中文相互转换的方法示例
2018/12/18 Python
python使用PIL和matplotlib获取图片像素点并合并解析
2019/09/10 Python
关于tensorflow的几种参数初始化方法小结
2020/01/04 Python
Keras Convolution1D与Convolution2D区别说明
2020/05/22 Python
详解CSS3伸缩布局盒模型Flex布局
2018/08/20 HTML / CSS
美国男士内衣品牌:Tommy John
2017/12/22 全球购物
巴西最大的运动品牌:Olympikus
2020/07/14 全球购物
监理员的岗位职责
2013/11/13 职场文书
工艺工程师工作职责
2013/11/23 职场文书
美化环境标语
2014/06/20 职场文书
幼儿教师年度个人总结
2015/02/05 职场文书
幼儿园开学家长寄语(2015秋季)
2015/05/27 职场文书
六五普法心得体会2016
2016/01/21 职场文书
2019西餐厅创业计划书范文!
2019/07/12 职场文书
用python自动生成日历
2021/04/24 Python
httpclient调用远程接口的方法
2022/08/14 Java/Android