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中的二维数组的操作方法
May 02 Python
浅谈python对象数据的读写权限
Sep 12 Python
Python 2.7中文显示与处理方法
Jul 16 Python
python 拼接文件路径的方法
Oct 23 Python
在python中利用最小二乘拟合二次抛物线函数的方法
Dec 29 Python
Python3 log10()函数简单用法
Feb 19 Python
详解Python locals()的陷阱
Mar 26 Python
Python实现动态循环输出文字功能
May 07 Python
Python学习之路之pycharm的第一个项目搭建过程
Jun 18 Python
基于Python下载网络图片方法汇总代码实例
Jun 24 Python
Python类绑定方法及非绑定方法实例解析
Oct 09 Python
python爬取代理ip的示例
Dec 18 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
精致的人儿就要挑杯子喝咖啡
2021/03/03 冲泡冲煮
一个用于MySQL的PHP XML类
2006/10/09 PHP
一个PHP的String类代码
2010/04/20 PHP
PHP中读写文件实现代码
2011/10/20 PHP
php中echo()和print()、require()和include()等易混淆函数的区别
2012/02/22 PHP
php中stdClass的用法分析
2015/02/27 PHP
php双层循环(九九乘法表)
2017/10/23 PHP
php微信公众号开发之二级菜单
2018/10/20 PHP
laravel5.5添加echarts实现画图功能的方法
2019/10/09 PHP
用RadioButten或CheckBox实现div的显示与隐藏
2013/09/21 Javascript
jquery实现的一个文章自定义分段显示功能
2014/05/23 Javascript
20条学习javascript的编程规范的建议
2014/11/28 Javascript
JQuery节点元素属性操作方法
2015/06/11 Javascript
简单介绍jsonp 使用小结
2016/01/27 Javascript
AngularJs bootstrap搭载前台框架——js控制部分
2016/09/01 Javascript
js判断浏览器是否支持严格模式的方法
2016/10/04 Javascript
JavaScript实现倒计时跳转页面功能【实用】
2016/12/13 Javascript
JavaScript中的call和apply的用途以及区别
2017/01/11 Javascript
微信小程序(六):列表上拉加载下拉刷新示例
2017/01/13 Javascript
js实现本地时间同步功能
2017/08/26 Javascript
使用vue实现grid-layout功能实例代码
2018/01/05 Javascript
详解如何使用router-link对象方式传递参数?
2019/05/02 Javascript
详解js实时获取并显示当前时间的方法
2019/05/10 Javascript
Vue.js实现大转盘抽奖总结及实现思路
2019/10/09 Javascript
微信小程序地图实现展示线路
2020/07/29 Javascript
python函数中return后的语句一定不会执行吗?
2017/07/06 Python
python使用pil进行图像处理(等比例压缩、裁剪)实例代码
2017/12/11 Python
python导入不同目录下的自定义模块过程解析
2019/11/18 Python
Python模拟登录和登录跳转的参考示例
2020/10/30 Python
喜诗官方在线巧克力店:See’s Candies
2017/01/01 全球购物
PacSun官网:加州生活方式服装、鞋子和配饰
2018/03/10 全球购物
数学教学随笔感言
2014/02/17 职场文书
关爱留守儿童倡议书
2014/04/15 职场文书
2015年事业单位工作总结
2015/04/27 职场文书
2015年信息化建设工作总结
2015/07/23 职场文书
浅谈Mysql多表连接查询的执行细节
2021/04/24 MySQL