python中操作文件的模块的方法总结
作者:小妮浅浅 时间:2022-02-01 04:56:14
在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)
来源:https://www.py.cn/jishu/jichu/23592.html
标签:python,操作文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python callable()函数用法实例分析
2021-12-14 00:28:52
Python代码打开本地.mp4格式文件的方法
2021-08-27 21:43:04
Python函数默认参数常见问题及解决方案
2023-04-03 15:42:42
Python+selenium实现自动循环扔QQ邮箱漂流瓶
2021-07-12 23:46:28
深入学习Python中的上下文管理器与else块
2023-05-11 08:57:53
MySQL中与NULL值有关的疑难问题
2008-11-24 12:47:00
python获取糗百图片代码实例
2022-09-10 01:44:01
如何基于Python + requests实现发送HTTP请求
2022-04-17 09:27:09
python图形开发GUI库pyqt5的基本使用方法详解
2022-07-08 02:31:55
![](https://img.aspxhome.com/file/2023/4/70884_0s.png)
python GUI库图形界面开发之pyinstaller打包python程序为exe安装文件
2021-03-29 23:02:58
![](https://img.aspxhome.com/file/2023/8/127078_0s.png)
python实现windows下文件备份脚本
2021-05-06 06:32:41
Python办公自动化批量处理文件实现示例
2021-01-12 01:11:24
![](https://img.aspxhome.com/file/2023/6/112096_0s.png)
CentOS下安装python3.5+scrapy的方法步骤
2022-07-17 20:01:42
python如何爬取网页中的文字
2021-04-05 03:55:53
python读取excel进行遍历/xlrd模块操作
2022-11-09 18:44:51
![](https://img.aspxhome.com/file/2023/0/128290_0s.jpg)
c++与python实现二分查找的原理及实现
2021-11-23 21:09:06
![](https://img.aspxhome.com/file/2023/3/128173_0s.gif)
解决方案,而不是功能
2011-01-30 18:11:00
![](https://img.aspxhome.com/file/UploadPic/20111/30/01-63s.jpg)
python3使用print打印带颜色的字符串代码实例
2022-01-20 10:30:49
sp_executesql 使用复杂的Unicode 表达式错误的解决方法
2012-01-29 17:58:52
pycharm 复制代码出现空格的解决方式
2023-08-27 12:36:37
![](https://img.aspxhome.com/file/2023/2/99232_0s.jpg)