Python file IO

1. File and IO

  • Variable, and object sequence data is stored temporarily, it will be lost after the end of the program, the data for a long time to save the program, you need to save data program to a disk file.
  • Python file objects and provides built, built directory module operation, it is easy to save the data to a file by means of these techniques, to achieve long-term storage of data.

2. The basic file operations

  • Python built-in file (File) object, creating open () method through the built-in open file object, and then do some basic file operations by providing a method of the object

2.1 to create and open files

  • Python want to operate in the file you need to create or open the specified file and creates a file object is achieved by the built-in function open ().
open()语法:
file = open(filename[,mode[,buffering]])

参数说明:
file:被创建的文件对象
filename:要创建或打开文件名称,需要使用单引号或双引号括起来,如打开文件与当前目录在同目录,直接写文件名字即可,否则需要写完整路径
mode:可选参数,指定文件打开模式如下图
buffering:用于指定读写文件的缓冲模式,值为0表示表达式不缓存;值为1表达表达式缓存;大于1,表示缓冲区大小,默认缓存模式

Here Insert Picture Description

# 创建文件,正常情况open()打开不存在文件会报错
# 指定mode的参数值为w,w+,a,a+,打开文件不存在,自动创建新的文件
file = open('yxy.txt','w')

# 文件打开,前提文件已经创建,如没有会报错
file1 = open('yxy.txt','r')
print(file1)
'''
输出:<_io.TextIOWrapper name='yxy.txt' mode='r' encoding='cp936'>
CP936其实就是GBK,IBM在发明Code Page的时候将GBK放在第936页,所以叫CP936
'''

# 打开指定编码文件,默认采用GBK编码,如果被打开不是GBK,抛出异常
# 方法一:直接修改文件编码
# 方法二:open()打开文件指定编码格式,推荐使用
file3 = open('yy.txt','w',encoding='utf-8')
print(file3)
'''
输出:<_io.TextIOWrapper name='yy.txt' mode='w' encoding='utf-8'>
'''

2.2 close the file

  • Open the file to be closed in a timely manner, so as to avoid unnecessary damage to the file, close the file using the close () method implementation
  • close () method to refresh the information has not been written to the buffer, and then close the file, so you can not write to the contents of the file is written to the file. After closing the file, the writing operation can not be performed.
# file.closed 查看文件打开关闭状态,为关闭显示False,关闭显示True
file4 = open('message.txt','w',encoding='utf-8')            # 创建打开文件
print('关闭前',file4.closed)                                # 输出:False
file4.close()                                               # 关闭文件
print('关闭后',file4.closed)                                 # 输出:True

# 文件读写时有可能产生IOError,一旦出错,file4.close()就不会调用
# 保障无论是否出错都能正确关闭文件,可以使用try-finally实现
try:
    file4 = open('message.txt', 'r', encoding='utf-8')
    print(file4)
    '''
    <_io.TextIOWrapper name='message.txt' mode='r' encoding='utf-8'>
    '''
    print('关闭前', file4.closed)                          # 输出:关闭前 False
finally:
    file4.close()
    print('关闭后', file4.closed)                          # 输出:关闭后 True

2.3 use to open the file with statement

  • Open the file, even if you close, forget to turn off may cause unexpected problems. Python provides with the statement, file handling, if an exception is thrown, that closed after the implementation of the file has been opened with the statement
with基本语法: 
with expression as target:
      with-body

参数说明:
expression:指定表达式,打开文件open()函数
target:指定变量,将expression结果保存到该变量中
with-body:指定with语句体,直接使用pass语句代替
with open('yxy.txt','r') as wfile:
    print(wfile.closed)                      # 输出:False
print(wfile.closed)                          # 输出:True

2.4 to read the file

  • Python open the file, you can write the additional content, you can read the contents of the file
read()语法:
file.read([size]) 说明:file打开文件对象;size可选参数指定读取字符个数,省略一次读取所内容

注意:调用read()方法读取内容前提,打开文件指定打开模式为r(只读)或r+(读写)否则会抛出异常
''io.UnsupportedOperation: not readable''
# yxy.txt文件内容
'''
11111111
22222222
33333333
44444444
'''
with open('yxy.txt','r') as wfile:
    string = wfile.read()
print(string)
'''
输出:
11111111
22222222
33333333
44444444
'''

# 读取一行
with open('yxy.txt','r') as wfile:
    str = wfile.readline()
    print(str)
'''
输出:11111111
'''

# 读取所有
with open('yxy.txt','r') as wfile:
    str1 = wfile.readlines()
    print(str1)
'''
输出:['11111111\n', '22222222\n', '33333333\n', '44444444']
'''

# 运行可以看到readlines()返回是一个字符串,如果文件较大,采用这中方法输出会很慢,
# 可以将列表内容逐行输出
with open('yxy.txt','r') as wfile:
    str1 = wfile.readlines()
    for message in str1:
        print(message.rstrip())     # 自带换行符 在换行 两个换行符 使用rstrip()删掉最右侧一个换行符
'''
输出:
11111111
22222222
33333333
44444444
'''

2.5 File Write

  • Call to write () method writes the contents of the premise to the file, open file, specify the open mode w (write), or a (additional), otherwise it will throw io.UnsupportedOperation: not writable
  • w mode opens the file overwrite the previous file, caution is recommended to use a (additional write file opened), the end of the file is written to a file
with open('3.txt','a',encoding='utf-8') as fp:
    # 只能写入一个字符串
    # fp.write("333")
    # 把列表元素(必须是字符串)写入文件
    data = ['111','222','333','444']
    # 添加换行符
    data = [line+'\n' for line in data]
    fp.writelines(data)
    # 刷新缓冲区【加速数据的流动,保证缓冲区的流畅】
    # close()关闭文件也会刷新缓冲区
    fp.flush()

2.6 moves the file pointer

file.seek(offset[,whence])

注意:使用seek()方法时,offfset的值按照一个汉字占两个字符,英文和数字占一个字符计算
参数说明:

file:表示已经打开的文件对象
offset:用于指针移动的字符个数,其具体位置与whence有关
whence:文件指针的位置,可以参数,值可以是
SEEK_SET or 0 表示文件开头位置,默认值
SEEK_CUR or 1 表示当前位置(不能使用)
SEEK_END or 2 表示末尾位置(不能使用)
# 1.txt内容:hello world
with open('1.txt','r',encoding='utf-8') as fp:
    # 移动到hello后的空格位置
    fp.seek(5)
    print(fp.read(3))    # 输出:wo
    # 移动到开头
    fp.seek(0)
    print(fp.read(5))    # 输出:Hello
    # 显示当前指针位置
    print(fp.tell())     # 输出5

3. Directory Operations

  • Directory called folder to save the file layered, different categories can store files directory
  • Python os module is built-in module associated with the operating system and file system functions, the module execution result of the statement is usually associated with an operating system, running on different operating systems, it may be to different results
  • Common directory operations are mainly determine whether a directory exists, create directories, delete directories and directory traversal and other

3.1 os module and os.path

  • Python, built os.path os module and sub-module, a file or directory operation, may also be used to import the os module submodule os.path
import os

# 获取操作系统类型 ,nt表示Windows操作系统,
# posix表示linux、Unix或Mac OS操作系统
print(os.name)       # 输出:nt

# 获取当前系统换行符
print(os.linesep)    # 输出:'\r\n' PyCharm貌似不显示,自动换行了

# 获取操作系统使用路径分隔符
print(os.sep)        # 输出:\

3.2 Path

  • Positioning a string file or directory is called a path. Program development, design two routes: one is a relative path, an absolute path to another

3.2.1 relative path

  • Work path: refers to the current directory between 2025 resides. Python, provide getcwd () function to get through the os module current working directory
  • Python, the file path to specify the separator whale "," transferred, i.e. the path '' '' with '' \ '', may be further separated paths '' '' instead of using the '/', or R & lt before the letters path string (or R), represents the path delimiter can be used directly without being escaped ""
import os
print(os.getcwd())   # 输出:E:\千锋教育\第24天_文件和目录\代码 
相对路径就是依赖于当前工作目录,如在当前工作目录下,有一个名称为yxy.txt的文件,打开这个文件可以直接写文件名,如果在当前工作目录下,
有一个子目录demo,并在该目录保存文件yxy.txt,那么打开这个文件写上''demo/yxy.txt''
# 直接打开yxy.txt文件
with open('yxy.txt','r') as rfile:
	pass

# 打开相对路径demo/yxy.txt文件
with open("demo/yxy.txt",'r') as rfile:
    pass

# 使用r打开相对路径
with open(r"demo\yxy.txt",'r') as rfile:
    pass

3.2.2 absolute path

Absolute path: the actual path of the file, does not depend on the current working directory

os.path.abspath(path)
# 获取绝对路径
import os
print(os.path.abspath(r"yxy.txt"))        
'''
输出:E:\千锋教育\第24天_文件和目录\代码\yxy.txt
'''

3.3.3 stitching path

  • Want to two or more circuit path stitched together comprise a new path, using os.path module join () function to achieve
  • Using the os.path.join () function stitching path, and does not detect whether the real path
os.path.join(path1[,path2[,......]])
# 拼接路径
import os
print(os.path.join(r"E:\千锋教育\第24天_文件和目录\代码",r"demo\message.txt"))
'''
输出:E:\千锋教育\第24天_文件和目录\代码\demo\message.txt
注意:该路径目录并不存在
'''
# 使用join()函数,要拼接路径,存在多个绝对路径,从左到右顺序最后一次出现为准,该路径之前参数都会被忽略
print(os.path.join(r'E:\code',r'C:\\','demo'))    # 输出:C:\\demo

3.4 determine whether a directory exists

  • Python, it is sometimes necessary to determine whether a given directory exists, the module exists os.path use () function
os.path.exists(path)

参数说明:
path:为要判断的目录,可以采用相对路径,也可以采用绝对路径
返回值:目录存在返回True,不存在返回False
import os
print(os.path.exists(r'E:\千锋教育\第24天_文件和目录\代码\demo\message.txt'))  # 输出:False
print(os.path.exists(r'E:\千锋教育\第24天_文件和目录\代码'))                   # 输出:True

3.5 Create a directory

  • In Python, os module provides two functions to create a directory, for creating a directory; another to create a multi-level directory

3.4.1 Creating a directory

  • You can only create a directory, use the os module provides the mkdir () function to achieve
  • You can specify only the last stage to create a directory, if the parent directory does not exist, an exception is thrown FileNotFoundError
os.mkdir(path,mode=0o777)

参数说明:
path:指定创建的目录,可以使用绝对路径,也可以使用相对路径
mode:指定数值模式,默认值0777,该参数在非UNIX系统上无效或被忽略
# 创建E:\demo目录
import os
if os.path.exists(r'E:\demo') == True:
    '''创建目录存在,执行mkdir命令会抛出FileExistsError异常'''
    pass
else:
    print('E:\demo没有存在已创建')
    os.mkdir(r'E:\demo')

3.4.2 create multi-level directory

  • Create multi-level directory, use the os module provides makedirs () function, which recursively create directories
os.makedirs(name,mode=0o777)
# 创建创建E:\demo\1\2\3\4目录
import os
path = r'E:\demo\1\2\3\4'          # 指定创建目录
if not os.path.exists(path):       # 判断目录是否存在
    os.makedirs(path)              # 创建目录
    print('目录创建成功')
else:
    print('该目录已存在')

3.6 delete the directory

  • Delete the directory using the os module provides rmdir () function to achieve
  • By the time rmdir function deletes a directory only if you want to delete empty directories only work ()
 os.rmdir(path)

# 删除E:\demo\1\2\3目录下的4目录
# 再次运行抛出异常:FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'E:\\demo\\1\\2\\3\\4'
# 说明该目录已经删除,只能删除空列表
import os
os.rmdir(r'E:\demo\1\2\3\4')
  • Use rmdir () function can only delete empty directories, delete function if you want to achieve a non-empty directory, use the built-in Guarantee rmtree Python module shutil of ()
import shutil
shutil.rmtree(r'E:\demo')     # 删除demo及下面目录及其内容

3.7 directory traversal

  • Python, for all directories under the specified directory (including subdirectories) and file browser again
  • Python, directory traversal function os module walk () function is used to implement
os.walk(top[,topdown][,onerror][,followlinks])

参数说明:
top:指定要遍历内容的根目录
topdown:可选参数,用于指定遍历顺序,如果值为True,表示自上而下遍历(即先遍历根目录);
         如果值为False,表示自下而上遍历(即先遍历最后一级子目录),默认值为True
onerror:可选参数,指定错误处理方式,默认忽略,如果不想忽略也可以指定一个错误处理函数,通常情况下默认
followlinks:可选参数,默认情况下,walk()函数不会向下转换成解析到目录的符号链接,
             将该参数值设置为True,表示用于指定在支持系统上访问由符号链接指定的目录
返回值:返回3个元素(dirpaht,dirnames,filenames)元组组成的生成器对象;
       dirpath:表示当前遍历路径,是一个字符串
       dirnames:表示当前路径下的包含的子目录是一个列表;
       filenames:表示当前路径下包含的文件也是一个列
# 遍历E盘所有文件目录
import  os

p = r'E:\\'
print('[',p,']目录下包含的文件和目录')
for root,dirs,files in os.walk(p,topdown = True):         # root根目录 dirs路径 filesw文件 遍历指定目录从哈桑倒下
    for name in dirs:
        print(os.path.join(root,name))
    for name in files:
        print('\t',os.path.join(root,name))
'''
输出:
E:\\明日科技\Python项目开发案例\23\wechat_robot\.git\refs
	 E:\\明日科技\Python项目开发案例\23\wechat_robot\.git\COMMIT_EDITMSG
	 E:\\明日科技\Python项目开发案例\23\wechat_robot\.git\config
	 E:\\明日科技\Python项目开发案例\23\wechat_robot\.git\description
	 E:\\明日科技\Python项目开发案例\23\wechat_robot\.git\HEAD
	 E:\\明日科技\Python项目开发案例\23\wechat_robot\.git\index
	 E:\\明日科技\Python项目开发案例\23\wechat_robot\.git\hooks\applypatch-msg.sample
	 ...
'''

4. Advanced file operations

  • In addition to the built-in Python os module can operate on the directory, you can also file some advanced operations, as follows
    Here Insert Picture Description

4.1 Delete Files

  • No built-in Python function to delete files, delete files offer a built-in function remove the os module ()
os.remove(path)
# 删除yxy.txt,删除不存在文件抛出异常
# FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'yxy.txt'
import os
os.remove('yxy.txt')

4.2 rename files and directories

  • os module provides functions rename rename files and directories (), if the path specified when the file, rename files; If the specified path to the directory, rename a directory
os.rename(src,dst)
# 重名名文件于目录一样
import os

src = r'E:\千锋教育\第24天_文件和目录\代码\1.txt'   # 要重名的文件
dst = r'E:\千锋教育\第24天_文件和目录\代码\2.txt'   # 重命名后的文件
if os.path.exists(src):                           # 判断文件是否存在
    os.rename(src,dst)                            # 从命名
    print('文件重命名完毕!')
else:
    print('文件不存在')

Get file information Basic 4.3

  • After the computer create a file, the file itself will contain some information through the os module stat () function to obtain the basic information file
os.stat(path)

Here Insert Picture Description

import os                                        # 导入os模块
if os.path.exists('2.txt'):                      # 判断文件是否存在
    fileinfo = os.stat('2.txt')                  # 获取文件的基本信息
    print('文件完整路径',os.path.abspath('2.txt'))# 获取文件的完整路径
    print('文件大小:',fileinfo.st_size,"字节")   # 输出文件基本信息
    print('最后一次修改时间',fileinfo.st_mtime)
'''
输出:
文件完整路径 E:\千锋教育\第24天_文件和目录\代码\2.txt
文件大小: 11 字节
最后一次修改时间 1584004049.6943874
'''
Published 21 original articles · won praise 3 · Views 2129

Guess you like

Origin blog.csdn.net/Sky_Mazarine/article/details/104819308