管理电脑文件及文件夹
1、输出目录下所有文件及文件夹
(1)python操作系统接口模块 https://docs.python.org/zh-cn/3.7/library/os.html
(2)获取当前python程序运行路径
import os print(os.getcwd())
print(os.path.join('xuexi python','01变量.py'))
(3)让python自动处理路径连接
(4)绝对路径与相对路径
(5)列出当前程序文件夹(及指定文件夹)下的所有文件和文件夹
import os print(os.getcwd()) print(os.path.join('xuexi python','01变量.py')) print(os.listdir()) #列出当前程序文件夹下的所有文件和文件夹(列表形式) for item in os.listdir(): #列出当前程序文件夹下的所有文件和文件夹 print(item,os.path.isdir(item)) print(os.listdir('E:\Youku Files')) #列出某个指定文件夹下的所有文件和文件夹(列表形式),英文路径 for file in os.scandir(): print(file.name,file.path,file.is_dir()) for file in os.scandir('E:\Youku Files'): print((file.name,file.path,file.is_dir()))
2.遍历、搜索文件及查询文件信息
(1)遍历文件夹
import os for dirpath, dirnames, files in os.walk('E:\Youku Files'): print(f'发现文件夹:{dirpath}') print(files) print('666good'.startswith('6')) print('good.txt'.endswith('t'))
(2)搜索、匹配文件名称
import glob print(glob.glob('1*.py')) print(glob.glob('?1*.py')) print(glob.glob('0[1-8]*.py')) print(glob.glob('[1,0]*.py')) print(glob.glob('[!1,6]*.py')) print(glob.glob('*.txt', recursive= True)) import fnmatch print(fnmatch.fnmatch('12函数使用.py','*使用.py')) print(fnmatch.fnmatch('12函数使用.py','[0-9]?函数??.py'))
(3)查询文件信息
import os import time import datetime for file in os.scandir(): print(file.name,file.stat(),file.stat().st_size/1024,time.ctime(file.stat().st_mtime)) file_time = file.stat().st_mtime print(datetime.datetime.fromtimestamp(file_time))
print(os.stat('01变量.py'))
import datetime normal_time = datetime.datetime.fromtimestamp(1498912896) print(normal_time,normal_time.year,normal_time.month,normal_time.day)
3、写入和读取文件;创建临时文件及文件夹
(1)读取文件
f = open('file.txt','r',encoding= 'utf-8') text = f.readlines() print(text) f.close
with open('D:\Desktop\快捷键.txt','r',encoding='ansi') as fi: text_fi = fi.readlines() print(text_fi)
(2)写入文件的两种模式(覆盖写 和 接着写)
with open('file.txt','w',encoding= 'utf-8') as f: text = 'a\nb\nc\n' f.write(text) f.write('d')
with open('file.txt','a',encoding= 'utf-8') as f: f.write('\n') f.write('good')
(3)创建临时文件和存储数据
from tempfile import TemporaryFile f = TemporaryFile('w+') f.write("good!good!") f.seek(0) data = f.readlines() print(data) f.close
with TemporaryFile("w+") as f: f.write("6jl very good") f.seek(0) data = f.readlines() print(data)
(4)创建临时文件夹
from tempfile import TemporaryDirectory with TemporaryDirectory() as tmp_folder: print(f'临时文件夹已创建:{tmp_folder}')
4、批量创建、复制、移动、删除、重命名文件及文件夹
(1)创建文件夹
import os if not os.path.exists("新文件夹"): os.mkdir('新文件夹')
import os os.makedirs('一/二')
(2)复制文件、文件夹
import shutil shutil.copy('file.txt','./新文件夹') shutil.copy('file.txt','./新文件夹/new_file.txt') shutil.copytree('一','新文件夹1')
(3)移动文件和文件夹
import shutil shutil.move('./新文件夹/file.txt','./一/二/file2.txt') shutil.move("./一/二/file2.txt",'新文件夹/') shutil.move("一",'新文件夹1/')
(4)重命名文件和文件夹
import os
os.rename('新文件夹','文件夹new') os.rename('./文件夹new/文件夹一','./文件夹new/wjj') os.rename('./文件夹new/file2.txt','./文件夹new/file_new.txt')
(5)删除文件,删除文件夹
import os os.remove("./文件夹new/file2.txt") import shutil shutil.rmtree('./文件夹new')
5、创建和解压压缩包
(1)读取压缩包
import zipfile with zipfile.ZipFile('xuexi.zip','r') as zipobj: for filename in zipobj.namelist(): print(filename.encode('cp437').decode('gbk'))
# 读取压缩包内文件信息
import zipfile with zipfile.ZipFile('xuexi.zip','r') as zipobj: for filename in zipobj.namelist(): info = zipobj.getinfo(filename) nfilename = filename.encode('cp437').decode('gbk') print(nfilename,info.file_size,info.compress_size,info)
(2)解压压缩包
import zipfile with zipfile.ZipFile('xuexi.zip','r') as zipobj: zipobj.extract('pyvenv.cfg','./xuexi/') zipobj.extractall(path= './xuexi')
(3)创建压缩包
import zipfile file_list = ['01.txt','./02.txt'] with zipfile.ZipFile('newzip.zip','w') as zipobj: for file in file_list: zipobj.write(file)