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多线程编程中的join函数使用心得
Sep 02 Python
Windows下Anaconda的安装和简单使用方法
Jan 04 Python
10 行 Python 代码教你自动发送短信(不想回复工作邮件妙招)
Oct 11 Python
学习python的前途 python挣钱
Feb 27 Python
Python如何爬取实时变化的WebSocket数据的方法
Mar 09 Python
python如何实现视频转代码视频
Jun 17 Python
用django-allauth实现第三方登录的示例代码
Jun 24 Python
解决Django连接db遇到的问题
Aug 29 Python
在notepad++中实现直接运行python代码
Dec 18 Python
学python爬虫能做什么
Jul 29 Python
python使用ctypes库调用DLL动态链接库
Oct 22 Python
Python time库的时间时钟处理
May 02 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
PHP5.3.1 不再支持ISAPI
2010/01/08 PHP
用php随机生成福彩双色球号码的2种方法
2013/02/04 PHP
PHP使用get_headers函数判断远程文件是否存在的方法
2014/11/28 PHP
php json中文编码为null的解决办法
2016/12/14 PHP
JavaScript 不只是脚本
2007/05/30 Javascript
一个奇葩的最短的 IE 版本判断JS脚本
2014/05/28 Javascript
javascript中String对象的slice()方法分析
2014/12/20 Javascript
jquery插件qrcode在线生成二维码
2015/04/26 Javascript
javascript关于运动的各种问题经典总结
2015/04/27 Javascript
jQuery中$this和$(this)的区别介绍(一看就懂)
2015/07/06 Javascript
JS文字球状放大效果代码分享
2015/08/19 Javascript
jquery实现浮动在网页右下角的彩票开奖公告窗口代码
2015/09/04 Javascript
针对JavaScript中this指向的简单理解
2016/08/26 Javascript
javascript cookie用法基础教程(概念,设置,读取及删除)
2016/09/20 Javascript
jQuery双向列表选择器DIV模拟版
2016/11/01 Javascript
easyui 中的datagrid跨页勾选问题的实现方法
2017/01/18 Javascript
详解angularjs结合pagination插件实现分页功能
2017/02/10 Javascript
基于node简单实现RSA加解密的方法步骤
2019/03/21 Javascript
vue使用axios上传文件(FormData)的方法
2019/04/14 Javascript
一行JavaScript代码如何实现瀑布流布局
2020/12/11 Javascript
[05:59]带你看看DPC的台前幕后
2021/03/11 DOTA
Python Tkinter GUI编程入门介绍
2015/03/10 Python
Python2和Python3中urllib库中urlencode的使用注意事项
2018/11/26 Python
python中enumerate() 与zip()函数的使用比较实例分析
2019/09/03 Python
使用tensorflow实现矩阵分解方式
2020/02/07 Python
H5离线存储Manifest原理及使用
2020/04/28 HTML / CSS
IWOOT美国:新奇的小玩意
2018/04/27 全球购物
TOWER London官网:鞋子、靴子、运动鞋等
2019/07/14 全球购物
大一学生职业生涯规划
2014/03/11 职场文书
入党自我鉴定
2014/03/25 职场文书
小学生中国梦演讲稿
2014/04/23 职场文书
根叔历年演讲稿
2014/05/20 职场文书
检查机关领导群众路线教育实践活动个人整改措施
2014/10/28 职场文书
党员年度个人总结
2015/02/14 职场文书
2015年扶贫帮困工作总结
2015/05/20 职场文书
HTML+CSS 实现顶部导航栏菜单制作
2021/06/03 HTML / CSS