python基础学习之组织文件
作者:shenmingik 发布时间:2022-04-08 21:47:23
一、Shutil 模块
shutil其实也就是shell模块。其中包含一些函数,可以让我们在python程序中复制、移动、改名和删除文件。
1.1 复制文件和文件夹
shutil.copy(source,destination):将路径source处的文件复制到路径destination处的文件夹。如果destination是一个文件名,那么它将作为被复制的新名字
shutil.copytree(source,destination):将路径source处的文件夹,包括它的所有文件和子文件夹,复制到路径destination处的文件夹。
import shutil
import os
current_path = os.getcwd()
# 拷贝test.txt 文件到temp1文件夹下
shutil.copy(current_path+"/test.txt",current_path+"/Python/temp1")
# 将temp1文件夹内容拷贝到temp2文件夹下,此时会创建temp2文件夹
shutil.copytree(current_path+"/Python/temp1",current_path+"/Python/temp2")
结果:
1.2 移动文件和文件夹
shutil.move(source,destination):将source处的文件夹移动到路径destination,并返回新位置的绝对路径的字符串
注:
如果destination指向一个文件夹,source文件将移动到destination中,并保持原来的文件名;
如果destination指向一个文件,这个文件就会被覆盖,注意!
如果destination指向一个不存在的文件夹,这个时候source里面的内容会移动到destination,source改名为destination
import shutil
import os
current_path = os.getcwd()
# 将temp2文件夹内的文件拷贝到temp中,并将temp2改名为temp
shutil.move(current_path+"/Python/temp2",current_path+"/temp")
结果:
1.3 删除文件和文件夹
os.unlink(path):将删除path处的文件
os.rmdir(path):将删除path处的文件夹。该文件夹必须为空,其中没有任何文件和文件夹
shutil.retree(path):将删除path处的文件夹,它包含的所有文件和文件夹都会被删除
import shutil
import os
current_path = os.getcwd()
shutil.rmtree(current_path+"/temp")
结果:
二、遍历文件
os.walk(path):通过传入一个路径
os.walk()在循环的每次迭代中,返回三个值:
1.当前文件夹名称的字符串
2.当前文件夹中子文件夹的字符串的列表
3.当前文件夹中文件的字符串的列表
import shutil
import os
current_path = os.getcwd()
for folder_name,sub_folders,file_names in os.walk(current_path):
print(folder_name+":")
# 加载当前文件路径下的所有子文件
for sub_folder in sub_folders:
print("\t"+folder_name+": "+sub_folder+"(dir)")
for file_name in file_names:
print("\t"+folder_name+": "+file_name)
输出(部分):
ubuntu@VM-0-2-ubuntu:~/python_file$ /usr/bin/python3 /home/ubuntu/python_file/Python/orignize_files.py
/home/ubuntu/python_file:
/home/ubuntu/python_file: .vscode(dir)
/home/ubuntu/python_file: Python(dir)
/home/ubuntu/python_file: test.cpp
/home/ubuntu/python_file: test.txt
/home/ubuntu/python_file: tempCodeRunnerFile.py
/home/ubuntu/python_file/.vscode:
/home/ubuntu/python_file/.vscode: db(dir)
/home/ubuntu/python_file/.vscode: .git(dir)
/home/ubuntu/python_file/.vscode: log(dir)
/home/ubuntu/python_file/.vscode: settings.json
/home/ubuntu/python_file/.vscode/db:
/home/ubuntu/python_file/.vscode/db: cpptips.db-wal
/home/ubuntu/python_file/.vscode/db: cpptips.db-shm
/home/ubuntu/python_file/.vscode/db: cpptips.db
/home/ubuntu/python_file/.vscode/.git:
/home/ubuntu/python_file/.vscode/.git: 6eb7a60f73d1a1d9bdf44f2e86d7f4cc_test.cpp
/home/ubuntu/python_file/.vscode/log:
/home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-19.log
/home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-16.log
/home/ubuntu/python_file/.vscode/log: cpptips.server.2021-05-17.log
/home/ubuntu/python_file/.vscode/log: cpptips.client.log
/home/ubuntu/python_file/.vscode/log: cpptips.server.log
/home/ubuntu/python_file/Python:
/home/ubuntu/python_file/Python: temp1(dir)
/home/ubuntu/python_file/Python: .git(dir)
/home/ubuntu/python_file/Python: README.md
/home/ubuntu/python_file/Python: hello_world.py
/home/ubuntu/python_file/Python: orignize_files.py
/home/ubuntu/python_file/Python: regex.py
/home/ubuntu/python_file/Python: file_mange.py
.........
.........
.........
三、压缩文件
利用zipfile模块中的函数,python程序可以创建和打开ZIP文件。
3.1 创建和添加ZIP文件
想要创建ZIP文件,必须用ZipFile
方法创建一个ZipFile对象。ZipFile对象在概念上和File对象相似。
之后,以写模式打开这个对象。调用write()
方法传入一个路径,python就会压缩该路径所指的文件,将它加到ZIP文件中。write的第一个参数是一个字符串,代表要添加的文件名。第二个参数是”压缩类型“参数,告诉计算机使用怎样的算法来压缩文件。一般来说,ZIP_DEFLATED就可以了。
import zipfile
import os
current_path = os.getcwd()
new_zip = zipfile.ZipFile("newZip","w")
new_zip.write("test.txt",compress_type=zipfile.ZIP_DEFLATED)
new_zip.write("test.cpp",compress_type=zipfile.ZIP_DEFLATED)
结果:
3.2 读取ZIP文件
当用ZipFile函数打开一个zip文件的时候,会返回一个ZipFile对象。之后调用这个对象的namelist()
方法就可以获得zip里面的压缩文件列表。
同时这个对象还有一个getinfo()方法,通过传入一个压缩文件名,就可以获得这个文件的一些信息。
import zipfile
import os
current_path = os.getcwd()
new_zip = zipfile.ZipFile("newZip","r")
files = new_zip.namelist()
for file in files:
info = new_zip.getinfo(file)
print("filename: "+file)
print("\tsize: "+str(info.file_size))
print("\tcompress_size: "+str(info.compress_size))
输出:
ubuntu@VM-0-2-ubuntu:~/python_file$ /usr/bin/python3 /home/ubuntu/python_file/Python/orignize_files.py
filename: test.txt
size: 26
compress_size: 26
filename: test.cpp
size: 30
compress_size: 28
3.3 解压缩ZIP文件
ZipFile对象的extractall方法从ZIP文件中解压缩所有的文件和文件夹,放到当前工作目录下:
import zipfile
import os
current_path = os.getcwd()
example_zip = zipfile.ZipFile("newZip","r")
# 解压
example_zip.extractall()
example_zip.close()
结果:
四、参考文献
AI Sweigart.Python编程快速上手——让繁琐工作自动化.人民邮电出版社.2016.07
来源:https://blog.csdn.net/shenmingxueIT/article/details/117076242


猜你喜欢
- 相信大家在日常学习或者是阅读英文文章的过程中,难免会出现几个不认识的单词,或者想快速翻译某段英文的意思。今天,利用Python爬虫等知识,教
- 本文实例为大家分享了Bootstrap进度条的具体实现代码,供大家参考,具体内容如下<!doctype html><htm
- 0. 前言周日在爬一个国外网站的时候,发现用协程并发请求,并且请求次数太快的时候,会出现对方把我的服务器IP封掉的情况。于是网上找了一下开源
- 一、下载SQL Server 2008 R2安装文件cn_sql_server_2008_r2_enterprise_x86_x64_ia6
- 如下所示:(x,y)为要转的点,(pointx,pointy)为中心点,如果顺时针角度为anglesrx = (x-pointx)*cos(
- 前言使用python做一个加密资料的软件,可加密应用程序、文件、压缩包等多种文件格式,不可直接加密文件夹,可以先用压缩包打包在加密。加密后的
- 偶尔会在python中看见这样一行代码:data = [x**2 for x in range(0, 5)]# 此时data = [0, 1
- 目录前言数据结构常规实现string转[]byte[]byte转string高效实现性能测试总结前言当我们使用go进行数据序列化或反序列化操
- Python的优点和缺点本节内容如下:Python的优点Python的缺点使用Python的知名网站Python的优点1. 简单 Pytho
- 本文实例讲述了golang中strconv.ParseInt函数用法。分享给大家供大家参考,具体如下:golang strconv.Pars
- HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,关于HTTP协议的详细内容
- 经过摸索和实践,我把自己的解决方法,写在下面: 说明: 我的Oracle客户端的版本是 oracle 9i, 安装client端的时候,不能
- 在SQL Server日常的函数、存储过程和SQL语句中,经常会用到不同数据类型的转换。在SQL Server有两种数据转换类型:一种是显性
- 继续还是工作中使用colly,不管是官网,还是网上的一些文章(其实90%就是把官网的案例抄过去),都是一样的格式,没有讲到post,测试了几
- pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。这篇文章重点给大家介绍
- 前言由于Django是 * 站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用:缓存,1.
- 数据库和操作系统一样,是一个多用户使用的共享资源。当多个用户并发地存取数据 时,在数据库中就会产生多个事务同时存取同一数据的情况。若对并发操
- 一、背景在kubernetes的世界中,很多组件仅仅需要一个实例在运行,比如controller-manager或第三方的controlle
- Python3中的map()、reduce()、filter() 这3个一般是用于对序列进行操作的内置函数,它们经常需要与 匿名函数 lam
- 本文实例讲述了php+mysql开发的最简单在线题库。分享给大家供大家参考,具体如下:题库,对于教育机构,学校,在线教育,是很有必要的,网上