python Task9 study notes 8.8 (54-clock in time-QQ)

Task9 File and file system
Open file:
open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True) Open file and return a stream. Raise OSError upon failure.
file: required, file path (relative or absolute path)
mode: optional, file open mode
buffering: set buffer
encoding: generally use utf8
errors: error level newline: distinguish newline characters
Common modes are:
'r': read only Way to open the file. The pointer of the file will be placed at the beginning of the file. This is the default mode.
'w': Open a file for writing only.
If the file already exists, open the file and start editing from the beginning.
That is, the original content will be deleted.
If the file does not exist, create a new file.
'x': Write mode, create a new file, if the file already exists, an error will be reported.
'a': Append mode, open a file for appending.
If the file already exists, the file pointer will be placed at the end of the file.
In other words, the new content will be written after the existing content.
If the file does not exist, create a new file for writing.
'b': Open the file in binary mode. Generally used for non-text files, such as pictures.
't': Open in text mode (default). Generally used for text files, such as txt.
'+': Read and write mode (can be added to other modes)

Open a file and return the file object. If the file cannot be opened, OSError will be thrown.

f = open('将进酒.txt')
print(f)
# <_io.TextIOWrapper name='将进酒.txt' mode='r' encoding='cp936'>

for each in f:
    print(each)

# 君不见,黄河之水天上来,奔流到海不复回。
# 君不见,高堂明镜悲白发,朝如青丝暮成雪。
# 人生得意须尽欢,莫使金樽空对月。
# 天生我材必有用,千金散尽还复来。
# 烹羊宰牛且为乐,会须一饮三百杯。
# 岑夫子,丹丘生,将进酒,杯莫停。
# 与君歌一曲,请君为我倾耳听。
# 钟鼓馔玉不足贵,但愿长醉不复醒。
# 古来圣贤皆寂寞,惟有饮者留其名。
# 陈王昔时宴平乐,斗酒十千恣欢谑。
# 主人何为言少钱,径须沽取对君酌。
# 五花马,千金裘,呼儿将出换美酒,与尔同销万古愁。

The file object method
fileObject.close() is used to close an opened file. The closed file can no longer be read or written, otherwise a ValueError will be triggered.
fileObject.read([size]) is used to read the specified number of characters from the file, and read all if it is not given or is negative.

f = open('将进酒.txt', 'r')
line = f.read(20)
print("读取的字符串: %s" % line)
# 读取的字符串: 君不见,黄河之水天上来,奔流到海不复回。

f.close()

fileObject.readline() reads the entire line, including the "\n" character:

f = open('将进酒.txt', 'r')
line = f.readline()
print("读取的字符串: %s" % line)
# 读取的字符串: 君不见,黄河之水天上来,奔流到海不复回。
f.close()

fileObject.readlines() is used to read all lines (until the end of EOF) and return a list, which can be processed by Python's for... in... structure.

f = open('将进酒.txt', 'r')
lines = f.readlines()
print(lines)

for each in lines:
    each.strip()
    print(each)

# 君不见,黄河之水天上来,奔流到海不复回。
# 君不见,高堂明镜悲白发,朝如青丝暮成雪。
# 人生得意须尽欢,莫使金樽空对月。
# 天生我材必有用,千金散尽还复来。
# 烹羊宰牛且为乐,会须一饮三百杯。
# 岑夫子,丹丘生,将进酒,杯莫停。
# 与君歌一曲,请君为我倾耳听。
# 钟鼓馔玉不足贵,但愿长醉不复醒。
# 古来圣贤皆寂寞,惟有饮者留其名。
# 陈王昔时宴平乐,斗酒十千恣欢谑。
# 主人何为言少钱,径须沽取对君酌。
# 五花马,千金裘,呼儿将出换美酒,与尔同销万古愁。

f.close()

fileObject.tell() returns the current position of the file, that is, the current position of the file pointer.

f = open('将进酒.txt', 'r')
line = f.readline()
print(line)
# 君不见,黄河之水天上来,奔流到海不复回。
pos = f.tell()
print(pos)  # 42
f.close()

fileObject.seek(offset[, whence]) is used to move the file reading pointer to the specified position. offset: The starting offset, that is, the number of bytes that need to be moved. If it is a negative number, it means starting from the last few digits. whence: Optional, the default value is 0. Define a parameter for offset to indicate the position from which to start the offset; 0 means counting from the beginning of the file, 1 means counting from the current position, and 2 means counting from the end of the file.

f = open('将进酒.txt', 'r')
line = f.readline()
print(line)
# 君不见,黄河之水天上来,奔流到海不复回。
line = f.readline()
print(line)
# 君不见,高堂明镜悲白发,朝如青丝暮成雪。
f.seek(0, 0)
line = f.readline()
print(line)
# 君不见,黄河之水天上来,奔流到海不复回。
f.close()

fileObject.write(str) is used to write the specified character string to the file, and the returned character length is written.

f = open('workfile.txt', 'wb+')
print(f.write(b'0123456789abcdef'))  # 16
print(f.seek(5))  # 5
print(f.read(1))  # b'5'
print(f.seek(-3, 2))  # 13
print(f.read(1))  # b'd'

Before the file is closed or the buffer is refreshed, the string content is stored in the buffer. At this time, you can't see the written content in the file. If the file open mode is with b, then when writing the file content, str (parameter) should be converted to bytes form using the encode method, otherwise an error will be reported: TypeError: a bytes-like object is required, not'str'.

fileObject.writelines(sequence) writes a list of sequence strings to the file. If you need to wrap the lines, you have to add a newline character\n for each line. Such as:

f = open('test.txt', 'w+')
seq = ['小马的程序人生\n', '老马的程序人生']
f.writelines(seq)
f.seek(0, 0)
for each in f:
    print(each)
    
# 小马的程序人生
# 老马的程序人生
f.close()

Concise with statement
Some objects define standard cleanup behavior, regardless of whether the system successfully uses it, once it is no longer needed, then this standard cleanup behavior will be executed. The keyword with statement can ensure that an object such as a file will execute its cleanup method correctly after use.

try:
    f = open('myfile.txt', 'w')
    for line in f:
        print(line)
except OSError as error:
    print('出错啦!%s' % str(error))
finally:
    f.close()

# 出错啦!not readable

After this code is executed, even if something goes wrong during processing, file f will always be closed.

try:
    with open('myfile.txt', 'w') as f:
        for line in f:
            print(line)
except OSError as error:
    print('出错啦!%s' % str(error))

# 出错啦!not readable    

2. The commonly used functions of files/directories in the OS module are
commonly used operating systems: Windows, Mac OS, Linu, Unix, etc. The underlying operating principles of these operating systems are different for file system access, so you It may be necessary to consider which file system modules to use for different systems... This approach is very unfriendly and troublesome, because it means that when your program operating environment changes, you have to modify a large number of them accordingly. Code to respond. With the OS (Operation System) module, we don't need to care about which module is used under which operating system, the OS module will help you select the correct module and call it.
os.getcwd() is used to return the current working directory.
os.chdir(path) is used to change the current working directory to the specified path.

import os

path = 'C:\\'
print("当前工作目录 : %s" % os.getcwd())
# 当前工作目录 : C:\Users\Administrator\PycharmProjects\untitled1
os.chdir(path)
print("目录修改成功 : %s" % os.getcwd())
# 目录修改成功 : C:\

listdir (path='.') returns a list of the names of files or folders contained in the folder specified by path.

import os

dirs = os.listdir()
for item in dirs:
    print(item)

os.mkdir(path) creates a single-level directory, and throws an exception if the directory already exists.

import os

if os.path.isdir(r'.\b') is False:
    os.mkdir(r'.\B')
    os.mkdir(r'.\B\A')

os.mkdir(r'.\C\A') # FileNotFoundError

os.makedirs(path) is used to recursively create multi-level directories. If the directory already exists, an exception will be thrown.

os.remove(path) is used to delete files in the specified path. If the specified path is a directory, OSError will be thrown.
For example, create the .\E\A\text.txt file first, and then delete it:

import os

print("目录为: %s" % os.listdir(r'.\E\A'))
os.remove(r'.\E\A\test.txt')
print("目录为: %s" % os.listdir(r'.\E\A'))

os.rmdir(path) is used to delete a single-level directory. Only if this folder is empty, otherwise, OSError is thrown.

import os

print("目录为: %s" % os.listdir(r'.\E'))
os.rmdir(r'.\E\A')
print("目录为: %s" % os.listdir(r'.\E'))

os.removedirs(path) deletes a directory recursively, trying to delete it layer by layer from the subdirectory to the parent directory, and throws an exception when the directory is not empty.
For example, first create the .\E\A directory and then delete it:

import os

print("目录为: %s" % os.listdir(os.getcwd()))
os.removedirs(r'.\E\A')  # 先删除A 然后删除E
print("目录为: %s" % os.listdir(os.getcwd()))

The os.rename(src, dst) method is used to name files or directories, from src to dst. If dst is an existing directory, OSError will be thrown.

Rename the test.txt file to test2.txt:

import os

print("目录为: %s" % os.listdir(os.getcwd()))
os.rename("test.txt", "test2.txt")
print("重命名成功。")
print("目录为: %s" % os.listdir(os.getcwd()))

os.system(command) runs the shell command of the system (converts a string into a command)
first creates an a.py file by itself, and then opens it by the shell command.

import os

path = os.getcwd() + '\\a.py'
a = os.system(r'python %s' % path)

os.system('calc')  # 打开计算器

os.curdir refers to the current directory (.)
os.pardir refers to the upper-level directory (...)
os.sep outputs the operating system-specific path separator (\ under win, / under Linux) os.linesep is used by the current platform The line terminator (\r\n under win, \n under Linux) os.name refers to the current operating system (including:'mac','nt')

import os

print(os.curdir)  # .
print(os.pardir)  # ..
print(os.sep)  # \
print(os.linesep)
print(os.name)  # nt

os.path.basename(path) removes the directory path and returns the file name alone. os.path.dirname(path) removes the file name and returns the directory path alone. , Each part of path2 is combined into a path name os.path.split(path) to split the file name and path, and return (f_path, f_name) tuple. If the directory is completely used, it will separate the last directory as the file name, and will not determine whether the file or directory exists.
os.path.splitext(path) separates the file name and extension, and returns a tuple of (f_path, f_name).

import os

# 返回文件名
print(os.path.basename(r'C:\test\lsgo.txt'))  # lsgo.txt
# 返回目录路径
print(os.path.dirname(r'C:\test\lsgo.txt'))  # C:\test
# 将目录和文件名合成一个路径
print(os.path.join('C:\\', 'test', 'lsgo.txt'))  # C:\test\lsgo.txt
# 分割文件名与路径
print(os.path.split(r'C:\test\lsgo.txt'))  # ('C:\\test', 'lsgo.txt')
# 分离文件名与扩展名
print(os.path.splitext(r'C:\test\lsgo.txt'))  # ('C:\\test\\lsgo', '.txt')

os.path.getsize(file) returns the size of the specified file, in bytes. os.path.getatime(file) returns the last access time of the specified file os.path.getctime(file) returns the creation time of the specified file os.path.getmtime(file) returns the latest modification time of the specified file in floating-point seconds , Can be converted with gmtime() or localtime() function of time module

import os
import time

file = r'.\lsgo.txt'
print(os.path.getsize(file))  # 30
print(os.path.getatime(file))  # 1565593737.347196
print(os.path.getctime(file))  # 1565593737.347196
print(os.path.getmtime(file))  # 1565593797.9298275
print(time.gmtime(os.path.getctime(file)))
# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=12, tm_hour=7, tm_min=8, tm_sec=57, tm_wday=0, tm_yday=224, tm_isdst=0)
print(time.localtime(os.path.getctime(file)))
# time.struct_time(tm_year=2019, tm_mon=8, tm_mday=12, tm_hour=15, tm_min=8, tm_sec=57, tm_wday=0, tm_yday=224, tm_isdst=0)

os.path.exists(path) determines whether the specified path (directory or file) exists os.path.isabs(path) determines whether the specified path is an absolute path
os.path.isdir(path) determines whether the specified path exists and is a directory os.path.isfile(path) determines whether the specified path exists and is a file os.path.islink(path) determines whether the specified path exists and is a symbolic link os.path.ismount(path) determines whether the specified path exists and is A hanging point os.path.samefile(path1,path2) determines whether the two paths path1 and path2 point to the same file.
3. Serialization and deserialization
Python's pickle module implements basic data sequence and deserialization.
Through the serialization operation of the pickle module, we can save the object information running in the program to a file for permanent storage.
Through the deserialization operation of the pickle module, we can create the object saved by the last program from the file. The most commonly used function in the pickle module is: pickle.dump(obj, file, [,protocol]) serializes the obj object into the opened file.
obj: The obj object that you want to serialize. file: file name.
protocol: The protocol used for serialization. If this item is omitted, it defaults to 0. If it is negative or HIGHEST_PROTOCOL, the highest protocol version is used. pickle.load(file) Serialize and read the objects in file. file: file name.

import pickle

dataList = [[1, 1, 'yes'],
            [1, 1, 'yes'],
            [1, 0, 'no'],
            [0, 1, 'no'],
            [0, 1, 'no']]
dataDic = {
    
    0: [1, 2, 3, 4],
           1: ('a', 'b'),
           2: {
    
    'c': 'yes', 'd': 'no'}}

# 使用dump()将数据序列化到文件中
fw = open(r'.\dataFile.pkl', 'wb')

# Pickle the list using the highest protocol available.
pickle.dump(dataList, fw, -1)

# Pickle dictionary using protocol 0.
pickle.dump(dataDic, fw)
fw.close()

# 使用load()将数据从文件中序列化读出
fr = open('dataFile.pkl', 'rb')
data1 = pickle.load(fr)
print(data1)
data2 = pickle.load(fr)
print(data2)
fr.close()

# [[1, 1, 'yes'], [1, 1, 'yes'], [1, 0, 'no'], [0, 1, 'no'], [0, 1, 'no']]
# {0: [1, 2, 3, 4], 1: ('a', 'b'), 2: {'c': 'yes', 'd': 'no'}}

Practice questions:
1. The open file that comes with Python can specify the text encoding. The encoding can specify the text in
open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True) coding

Guess you like

Origin blog.csdn.net/m0_45672993/article/details/107881803