python目录和文件的基本操作_超全!python的文件和目录操作总结

文件的基本读写

path = r'C:\Users\Brady\Documents\tmp'

with open(path + r'\demo.txt', 'r', encoding='utf-8') as f:

content = f.read()

print(content)

open()函数

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

Open file and return a corresponding file object. If the file cannot be opened, an OSError is raised.

open函数用于打开一个文件,并返回文件句柄.

文件打开的mode主要有以下几种方式:

mode

含义

'r'

读取(默认)

'w'

写入(会截断之前的文件内容)

'x'

写入(如果文件已经存在会产生异常)

'a'

追加,将新内容写入到文件末尾

'b'

二进制模式

't'

文本模式(默认)

'+'

更新,可读可写

这里关于newline做一个解释. newline是换行符,windows系统的换行符和类unix系统的换行符是不一样的. windows默认使用\r\n做为换行符. 而类unix系统使用\n作为换行符.

关于换行符的使用,文档给出了如下解释:

如果newline为None,则\r \n \r\n都会被识别为换行符,并统一翻译为\n.

如果newline为'',则直接返回源文件中的换行符

关于换行符\r\n 和\n 的历史要追溯到计算机出现之前的电传打印机.

\r的意思代表回车,也就是打印头回到初始位置.

\n的意思表示换行,也就是纸张往上卷一行.

在windows中保留了这种老传统. 真正的换行符需要\r\n

而类unix中则选择使用\n作为换行符

write()函数

with open(path+r'\demo2.txt','w',encoding='utf-8') as f:

content = 'this is a demo for write function'

res=f.write(content)

print(res)

file对应的方法

file.close(): 关闭文件

file.flush():讲缓冲区的内容立即写入文件

file.readline():读取整行

file.readlines():按行读取,并返回列表.可以设定读取的字节数

file.seek()设置游标位置

file.tell()显式当前游标位置

file.truncate()截取文件

目录相关操作

获取目录列表

with os.scandir(path2) as entries:

for item in entries:

print(item.name)

scandir()返回的是一个生成器.

同样也可以使用pathlib库.

enties = Path(path2)

for entry in enties.iterdir():

print(entry.name)

获取目录下的文件

for entry in os.listdir(basepath):

if os.path.isfile(os.path.join(basepath,entry)):

print(entry)

with os.scandir(basepath) as entries:

for entry in entries:

if entry.is_file():

print(entry.name)

base_path = Path(basepath)

for entry in base_path.iterdir():

if entry.is_file():

print(entry.name)

base_path = Path(basepath)

files_in_basepath = (entry for entry in base_path.iterdir() if entry.is_file())

for item in files_in_basepath:

print(item.name)

以上四种办法都可以.

获取子目录

for entry in os.listdir(basepath):

if os.path.isdir(os.path.join(basepath,entry)):

print(entry)

with os.scandir(basepath) as entries:

for entry in entries:

if entry.is_dir():

print(entry.name)

base_path = Path(basepath)

for entry in base_path.iterdir():

if entry.is_dir():

print(entry.name)

获取文件属性

with os.scandir(basepath) as entries:

for entry in entries:

info = entry.stat()

print(entry.name,timestamp2datetime(info.st_mtime))

base_path = Path(basepath)

for entry in base_path.iterdir():

info = entry.stat()

print(entry.name,timestamp2datetime(info.st_mtime))

os.scandir()返回一个os.dirEntry对象. os.dirEntry对象大概有以下属性和方法:

name:文件(目录)名

path:文件(目录)路径

is_file()

is_dir()

stat()返回一个stat_result对象.

而stat_result对象又有N多关于文件的属性,比如时间戳相关的属性:

st_atime:最近访问时间

st_mtime:最近修改时间

st_ctime:创建时间

创建目录

在os和pathlib的模块中都包含了创建目录的函数.

os.mkdir() 创建单个子目录

os.makedirs() 创建多个目录,包括中间目录

Pathlib.Path.mkdir() 创建单个或者多个目录

创建单个目录

os.chdir(basepath)

if not os.path.exists(os.path.join(basepath,'c')):

os.mkdir('c')

base_path = Path(basepath+r'\d')

try:

base_path.mkdir()

except FileExistsError :

pass

通过os.mkdir()和Path.mkdir()都可以创建单个目录. 如果目录已经存在,则会报FileExistsError异常. 也可以使用exist_ok=True 参数来忽略这个异常

创建多个目录

可以使用os.makedirs()来创建包含中间目录在内的所有目录,类似mkdir -p

os.makedirs('2020/3/2')

也可以使用Path.mkdir()方法来创建多层目录.只需要指定parents=True比如

from pathlib import Path

p = Path('2018/10/05')

p.mkdir(parents=True, exist_ok=True)

文件名的模式匹配

使用字符串方法

python有一些内置的修改和操作字符串的方法,在操作文件名的时候,可以先遍历拿到文件名,然后使用字符串的方式进行匹配.

for item in os.listdir(basepath):

if item.endswith('.txt'):

print(item)

使用fnmatch库

另外还可以使用fnmatch库,fnmatch库支持类unix的通配符.

通配符

含义

*

匹配所有字符

?

匹配任意一个字符

[seq]

匹配一个序列

[!seq]

匹配一个不包含seq的序列

import fnmatch

for item in os.listdir(basepath):

if fnmatch.fnmatch(item,"*.txt"):

print(item)

使用glob库

总的来说,glob库和fnmatch库差不多,但是glob库提供了递归功能,可以查询目录下子目录的文件名.

glob.glob(pathname, *, recursive=False)

另外在pathlib中也提供了类似glob的方法.

总结:

函数

描述

startswith()

是否以一个特定的序列开头

endswith()

是否以一个特定的序列结尾

dnmatch.fnmatch(filename,pattern)

测试文件名是否满足正则表达式

glob.glob()

返回匹配的文件列表

pathlib.Path.glob()

返回一个匹配该模式的生成器对象

遍历和处理文件

os.walk(top, topdown=True, οnerrοr=None, followlinks=False)

os.chdir(basepath)

for dirpath,dirname,files in os.walk('.'):

print(f'found directory:{dirpath}')

for filename in files:

print(filename)

walk()方法返回一个三元组(dirpath,dirnames,filenames)

dirpath:当前目录的名称

dirnames:当前目录中子目录的列表

当前目录中文件的列表

创建临时文件和目录

临时文件和临时目录就是程序运行时创建,在程序运行结束之后会自动删除的文件和目录.

可以使用tempfile模块来进行操作.

from tempfile import TemporaryFile

from tempfile import TemporaryDirectory

fp = TemporaryFile('w+t')

fp.write('hello world')

fp.seek(0)

data = fp.read()

print(data)

fp.close()

with TemporaryFile('w+t',encoding='utf-8') as tf:

tf.write('hello world')

tf.seek(0)

print(tf.read())

tmp=''

with TemporaryDirectory() as tmpdir:

print("create a temp directory{0}".format(tmpdir))

tmp = tmpdir

print(os.path.exists(tmp))

print(os.path.exists(tmp))

临时文件作为一个临时的硬盘上的缓存,一般不需要命名. 但是如果需要使用带文件名的临时文件时,可以使用tempfile.NamedTemporaryFile()

在windows平台下,临时文件一般存放在C:/TEMP或者C:/TMP. 其他平台上,一般存放顺序为/tmp,/var/tmp,/usr/tmp 如果以上路径都找不到的话,python会默认在当前目录中存放临时文件和临时目录.

注意,TemporaryFile()等方法也是支持with..in这种上下文管理器的.

删除文件和目录

删除文件

要删除单个文件有三种办法:pathlib.Path.unlink() , os.remove() 还有 os.unlink()方法

这里需要注意的是,os.remove()和os.unlink()没有什么区别. unlink是类unix系统中的早期叫法.

os.remove(os.path.join(basepath,'demo.txt'))

os.unlink(os.path.join(basepath,'demo2.txt'))

或者使用pathlink.Path.unlink()方法

from pathlib import Path

p = Path(basepath+r'\1-demo.txt')

p.unlink()

注意,以上方法只能删除文件,如果删除的不是文件而是目录的话,会报IsADirectoryError异常

删除目录或目录树

三个方法:

os.rmdir()

pathlib.Path.rmdir()

shutil.rmtree()

在os.rmdir()和pathlib.Path.rmdir()中,如果删除的是非空目录,会报OSError异常.

os.rmdir(os.path.join(basepath,'a'))

p = Path(basepath+r'\b')

p.rmdir()

如果想删除非空目录或者目录树的话,可以是用shutil.rmtree()方法

shutil.rmtree(os.path.join(basepath,'2020'))

复制,移动和重命名文件和目录

这里我们要使用到shutil模块,shutil模块提供了类似shell的一些功能.

复制文件

import os

import shutil

src = os.path.join(basepath,'0-demo.txt')

dst = os.path.join(basepath,'c')

shutil.copy(src,dst)

这个不需要多讲了,类似cp命令. 如果dst是文件,则覆盖原文件,如果dst是目录的话,则拷贝到该目录下.

copy()方法不会复制元数据. 如果要连文件信息等元数据一起复制的话,则需要使用copy2()方法.

复制目录

import os

import shutil

src = os.path.join(basepath,'c')

dst = os.path.join(basepath,r'd\bak')

shutil.copytree(src,dst)

这里需要注意的是,目标目录不能是已存在的目录. 而且在复制的时候,不带原目标目录的父目录.

说人话就是上面这段代码在执行的时候,只会讲c目录内的内容复制到bak目录里去.

移动文件和目录

import os

import shutil

src = os.path.join(basepath,'c')

dst = os.path.join(basepath,r'd\bak')

shutil.move(src,dst)

跟shell中的mv用法一样一样一样的. 如果目的目录存在,则会将源目录移动到目的目录中去. 如果目的目录不存在,那就是源目录的重命名.

重命名文件和目录

可是使用os模块中的rename()方法,也可以使用pathlib.Path.rename()方法.

os.chdir(basepath)

os.rename('3-demo.txt','demo3.txt')

p = Path('0-demo.txt')

p.rename('demo0.txt')

归档

所谓归档就是打包. 最常见的两种打包方式就是zip和tar.(嗯...不要说rar...)

读取zip文件

python提供了zipfile的内置模块用来处理zip文件.

import os

import zipfile

os.chdir(basepath)

with zipfile.ZipFile('d.zip','r') as zf:

filelist=zf.namelist()

bar_file_info = zf.getinfo('d/bak/0-demo.txt')

print(type(bar_file_info))

print(bar_file_info.file_size)

print(filelist)

提取zip文件

通过zipfile.extract()和zipfile.extractall()可以从zip文件中提取一个或多个文件.

with zipfile.ZipFile('d.zip','r') as zipobj:

zipobj.extract('d/bak/0-demo.txt')

zipobj.extractall(path=r'./zip/')

创建新的zip文件

直接使用write()方法就可以了.

file_list = []

for item in os.listdir():

if fnmatch.fnmatch(item,'*-demo.txt'):

file_list.append(item)

with zipfile.ZipFile('demo.zip','w') as zipobj:

for txt_file in file_list:

zipobj.write(txt_file)

tarfile库的操作

tar文件在linux中比较常用,可以使用gzip,bzip2和lzma等压缩方法进行压缩. python同样内置了tarfile库用于处理tar文件.

file_list = []

for item in os.listdir():

if fnmatch.fnmatch(item,'*-demo.txt'):

file_list.append(item)

# 创建一个tar包

with tarfile.open('demo.tar.gz',mode='w:gz') as tf:

for file_name in file_list:

tf.add(file_name)

# 读取tar包

with tarfile.open('demo.tar.gz',mode='r:gz') as tf:

for member in tf.getmembers():

print(member.name)

# 解压缩tar包

with tarfile.open('demo.tar.gz',mode='r:gz') as tf:

tf.extract('2-demo.txt',path=r'./d/demo')

tf.extractall(path=r'./d/extractall')

关于打开模式的解释,懒得翻译了.

mode

action

'r' or 'r:*'

Open for reading with transparent compression (recommended).

'r:'

Open for reading exclusively without compression.

'r:gz'

Open for reading with gzip compression.

'r:bz2'

Open for reading with bzip2 compression.

'r:xz'

Open for reading with lzma compression.

'x' or 'x:'

Create a tarfile exclusively without compression. Raise an FileExistsError exception if it already exists.

'x:gz'

Create a tarfile with gzip compression. Raise an FileExistsError exception if it already exists.

'x:bz2'

Create a tarfile with bzip2 compression. Raise an FileExistsError exception if it already exists.

'x:xz'

Create a tarfile with lzma compression. Raise an FileExistsError exception if it already exists.

'a' or 'a:'

Open for appending with no compression. The file is created if it does not exist.

'w' or 'w:'

Open for uncompressed writing.

'w:gz'

Open for gzip compressed writing.

'w:bz2'

Open for bzip2 compressed writing.

'w:xz'

Open for lzma compressed writing.

shutil库创建存档

shutil库的make_archive()方法同样可以创建归档.

shutil.make_archive(base_name, format[, root_dir[, base_dir[, verbose[, dry_run[, owner[, group[, logger]]]]]]])

shutil.unpack_archive(filename[, extract_dir[, format]])

shutil.make_archive(r'.\d\backup','tar',r'.\d')

shutil.unpack_archive(r'.\d\backup.tar')

吾码2016

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值