文件的基本读写

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:创建时间

创建目录

ospathlib的模块中都包含了创建目录的函数.

  • 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, onerror=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

超全!python的文件和目录操作总结的更多相关文章

  1. 【转】Python之文件与目录操作(os、zipfile、tarfile、shutil)

    [转]Python之文件与目录操作(os.zipfile.tarfile.shutil) Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读 ...

  2. Python之文件与目录操作及压缩模块(os、shutil、zipfile、tarfile)

    Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...

  3. Python之文件与目录操作(os、zipfile、tarfile、shutil)

    Python中可以用于对文件和目录进行操作的内置模块包括: 模块/函数名称 功能描述 open()函数 文件读取或写入 os.path模块 文件路径操作 os模块 文件和目录简单操作 zipfile模 ...

  4. Python之文件和目录操作

    1.文件基本操作 python内置了打开文件的函数open(),使用规则如下:   File_object=open(filename[,access_mode][,buffering]) Filen ...

  5. Python::OS 模块 -- 文件和目录操作

    os模块的简介参看 Python::OS 模块 -- 简介 os模块的进程管理 Python::OS 模块 -- 进程管理 os模块的进程参数 Python::OS 模块 -- 进程参数 os模块中包 ...

  6. Python中的文件和目录操作实现

    Python中的文件和目录操作实现 对于文件和目录的处理,虽然可以通过操作系统命令来完成,但是Python语言为了便于开发人员以编程的方式处理相关工作,提供了许多处理文件和目录的内置函数.重要的是,这 ...

  7. python文件及目录操作

    python文件及目录操作 读/写文件 新建/打开文件 写入 #举个例子,打开D:\test\data.txt #以写入模式打开文件 #如果test(上级目录)不存在则报错 #如果data.txt(文 ...

  8. 零基础学Python--------第10章 文件及目录操作

    第10章 文件及目录操作 10.1 基本文件操作 在Python中,内置了文件(File)对象.在使用文件对象时,首先需要通过内置的open() 方法创建一个文件对象,然后通过对象提供的方法进行一些基 ...

  9. Shell命令-文件及目录操作之pwd、rm

    文件及目录操作 - pwd.rm 1.pwd:显示当前所在位置信息 pwd命令的功能说明 pwd命令用于显示当前工作目录的绝对路径,以便在各个目录间来回切换. pwd命令的语法格式 pwd [OPTI ...

随机推荐

  1. 源码分析Dubbo服务消费端启动流程

    通过前面文章详解,我们知道Dubbo服务消费者标签dubbo:reference最终会在Spring容器中创建一个对应的ReferenceBean实例,而ReferenceBean实现了Spring生 ...

  2. ubuntu 更新源 或者 apt-get install 出错404 not found ,Failed to fetch

    1.考虑是不是能上网 2.用apt-get update ,然后再试试apt-get install 如果apt-get update 也出现很多 404 not found 或者 failed to ...

  3. iTOP-iMX6UL开发板-MiniLinux-CAN测试使用文档

    本文档介绍的是迅为iMX6UL开发板在 MiniLinux 系统环境下 iTOP-iMX6UL CAN 实验调试步骤.给用户提供了“can_libs.rar”.“can_tools.zip”和“can ...

  4. moco jar包下载

    http://repo1.maven.org/maven2/com/github/dreamhead/moco-runner/0.11.0/ 选择moco-runner-0.11.0-standalo ...

  5. E - Apple Tree(树状数组+DFS序)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  6. PAT甲级——1036 Boys vs Girls

    1036 Boys vs Girls This time you are asked to tell the difference between the lowest grade of all th ...

  7. Codeforces Round#615 Div.3 解题报告

    前置扯淡 真是神了,我半个小时切前三题(虽然还是很菜) 然后就开始看\(D\),不会: 接着看\(E\),\(dp\)看了半天,交了三次还不行 然后看\(F\):一眼\(LCA\)瞎搞,然后\(15m ...

  8. Excel-DNA项目只用1个文件实现Ribbon CustomUI和CustomTaskpane定制【VB.Net版】

    Excel-DNA项目中的自定义功能区和自定义任务窗格需要用到各种命名空间.添加所需文件,才能实现.后来我发现可以把所有代码都写在Class1.vb这个默认文件中. 大家可以在Visual Studi ...

  9. pycharm2018后版本执行Flask app.run()深坑

    在2018年以前的版本,以上配置在app.run()里面的内置方法

  10. 《C程序设计语言》练习 1-12

    #include<stdio.h> /*编写一个程序,以每行一个单词的形式打印其输入*/ main() { int c; c=getchar(); while(c!=EOF) { if ( ...