Deliberate practice: Python foundation -- Task07. File and file system

Keywords: Python encoding network Mac

background

We're going to use 17 days to "Deliberate practice of Python foundation" It is divided into the following tasks:

This is the clock in content of my 07 / 12 tasks. My idea of learning Python is to first get familiar with the overall syntax framework of Python and connect the knowledge points with my familiar programming language, and then slowly supplement the later problems to form my own knowledge structure.

Python basic syntax

1. Open file

  • open(file, mode='r ') receives two parameters: file name and mode, which are used to open a file and return the file object. If the file cannot be opened, an OSError will be thrown.

Open mode | perform operation

The complete syntax format is:

  • open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None)
  • File: required, file path (relative or absolute).
  • Mode: optional, file open mode
  • buffering: setting buffer
  • encoding: utf8 is generally used
  • errors: error level
  • newline: line break sensitive
f = open('Drink in.txt')
print(f)
# < Ou io. Textiowrapper name = 'wine will be served. TXT' mode ='r 'encoding ='cp936' >

for each in f:
    print(each)

# If you don't see it, the water of the Yellow River will come up in the sky and run to the sea.
# If you don't see it, the mirror in the high hall will be sad with white hair, and it will be like snow in the morning.
# When you are satisfied with your life, do not let the golden cup empty to the moon.
# I'll be useful if I'm born, but I'll come back when all the money is gone.
# When cooking sheep and slaughtering cattle for pleasure, you will have to drink 300 cups.
# CEN Fuzi, Danqiu Sheng, will bring in the wine. Don't stop drinking.
# A song with you, please listen to it for me.
# I hope you will never wake up when you are drunk for a long time.
# In ancient times, sages and sages were all lonely, only the drinkers kept their names.
# In the past, King Chen had a peaceful banquet and ten thousand wine fights.
# What do you mean by saying little? It's only a matter of buying and drinking.
# Five flower horse, thousands of gold and fur, hu'er is going to trade for wine, and sell with you forever sorrow.

2. Document object method

  • fileObject.close() is used to close an open file. The closed file can no longer be read or written, otherwise the ValueError error will be triggered.
f = open("Drink in.txt")
print('FileName:', f.name)  # FileName: will drink.txt
f.close()
  • fileObject.read([size]) is used to read the specified number of characters from the file. If it is not given or negative, all characters will be read.
f = open('Drink in.txt', 'r')
line = f.read(20)
print("Read string: %s" % line)
# Read string: you do not see, the Yellow River water sky, running to the sea never return.

f.close()
  • fileObject.readline() reads the entire line, including the "\ n" character.
f = open('Drink in.txt', 'r')
line = f.readline()
print("Read string: %s" % line)
# Read string: you do not see, the Yellow River water sky, running to the sea never return.
f.close()
  • fileObject.readlines() is used to read all lines (up to EOF) and return a list that can be processed by Python's for... in... Structure.
f = open('Drink in.txt', 'r')
lines = f.readlines()
print(lines)

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

# If you don't see it, the water of the Yellow River will come up in the sky and run to the sea.
# If you don't see it, the mirror in the high hall will be sad with white hair, and it will be like snow in the morning.
# When you are satisfied with your life, do not let the golden cup empty to the moon.
# I'll be useful if I'm born, but I'll come back when all the money is gone.
# When cooking sheep and slaughtering cattle for pleasure, you will have to drink 300 cups.
# CEN Fuzi, Danqiu Sheng, will bring in the wine. Don't stop drinking.
# A song with you, please listen to it for me.
# I hope you will never wake up when you are drunk for a long time.
# In ancient times, saints and sages were all lonely, only those who drank left their names.
# In the past, King Chen had a peaceful banquet and ten thousand wine fights.
# What do you mean by saying little? It's only a matter of buying and drinking.
# Five flower horse, thousands of gold and fur, hu'er is going to trade for wine, and sell with you forever sorrow.

f.close()
  • fileObject.tell() returns the current location of the file, that is, the current location of the file pointer.
f = open('Drink in.txt', 'r')
line = f.readline()
print(line)
# If you don't see it, the water of the Yellow River will come up in the sky and run to the sea.
pos = f.tell()
print(pos)  # 42
f.close()
  • Fileobject.seek (offset [, when]) is used to move the file read pointer to the specified location.
  • Offset: the start offset, that is, the number of bytes to move the offset. If it is a negative number, it means the last digit.
  • When: optional, the default value is 0. Define a parameter for offset to indicate the location from which to start offset; 0 represents the beginning of the file, 1 represents the beginning of the current location, and 2 represents the end of the file.
f = open('Drink in.txt', 'r')
line = f.readline()
print(line)
# If you don't see it, the water of the Yellow River will come up in the sky and run to the sea.
line = f.readline()
print(line)
# If you don't see it, the mirror in the high hall will be sad with white hair, and it will be like snow in the morning.
f.seek(0, 0)
line = f.readline()
print(line)
# If you don't see it, the water of the Yellow River will come up in the sky and run to the sea.
f.close()
  • fileObject.write(str) is used to write a specified string to a file. It returns the length of the characters 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 flushed, the string content is stored in the buffer. At this time, you cannot see the written content in the file.

If the file open mode has b, str (parameter) should be converted to bytes when writing the file content. Otherwise, an error will be reported: typeerror: a bytes like object is required, not 'STR'.

str = '...'
# Text = Unicode character sequence
# Equivalent to string type

str = b'...'
# Text = octet sequence (integer between 0 and 255)
# Byte literals are always prefixed with 'B' or 'B'; they produce an instance of a byte type, not a str type.
# Equivalent to byte []

Sample:

f = open('Drink in.txt', 'r+')
str = '\n Author: Li Bai'
f.seek(0, 2)
line = f.write(str)
f.seek(0, 0)
for each in f:
    print(each)

# If you don't see it, the water of the Yellow River will come up in the sky and run to the sea.
# If you don't see it, the mirror in the high hall will be sad with white hair, and it will be like snow in the morning.
# When you are satisfied with your life, do not let the golden cup empty to the moon.
# I'll be useful if I'm born, but I'll come back when all the money is gone.
# When cooking sheep and slaughtering cattle for pleasure, you will have to drink 300 cups.
# CEN Fuzi, Danqiu Sheng, will bring in the wine. Don't stop drinking.
# A song with you, please listen to it for me.
# I hope you will never wake up when you are drunk for a long time.
# In ancient times, saints and sages were all lonely, only those who drank left their names.
# In the past, King Chen had a peaceful banquet and ten thousand wine fights.
# What do you mean by saying little? It's only a matter of buying and drinking.
# Five flower horse, thousands of gold and fur, hu'er is going to trade for wine, and sell with you forever sorrow.
# Author: Li Bai

f.close()
  • fileObject.writelines(sequence) writes a list of sequence strings to the file. If you need to wrap lines, you need to add line breaks for each line by yourself \ n.
f = open('test.txt', 'w+')
seq = ['Program life of pony\n', 'The programmed life of the old horse']
f.writelines(seq)
f.seek(0, 0)
for each in f:
    print(each)
    
# Program life of pony
# The programmed life of the old horse
f.close()

3. Common function usage of files / directories in OS module

As we know, the common operating systems are: Windows, Mac OS, Linu, Unix, etc. the underlying access to the file system works differently, so you may need to consider which file system modules to use for different systems... , which is very unfriendly and troublesome, because it means that when your program running environment changes, you have to modify a lot of code accordingly.

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("Current working directory : %s" % os.getcwd())
# Current working directory: C: \ users \ administrator \ pychamprojects \ untitled1
os.chdir(path)
print("Directory modified successfully : %s" % os.getcwd())
# Directory modified successfully: C:\
  • os.listdir(path) returns a list of the names of the 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-layer 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 a multi-level directory and throw an exception if the directory already exists.
import os
os.makedirs(r'.\E\A')
  • os.remove(path) is used to delete files with the specified path. If the specified path is a directory, an OSError is thrown.
import os

print("Directory is: %s" % os.listdir(r'.\E\A'))
os.remove(r'.\E\A\test.txt')
print("Directory is: %s" % os.listdir(r'.\E\A'))
  • os.rmdir(path) is used to delete a single tier directory. Only if the folder is empty, otherwise, an OSError is thrown.
import os

print("Directory is: %s" % os.listdir(r'.\E'))
os.rmdir(r'.\E\A')
print("Directory is: %s" % os.listdir(r'.\E'))
  • Os.removediers (path) recursively deletes the directory, trying to delete it layer by layer from the subdirectory to the parent directory, and throwing an exception if the directory is not empty.
import os

print("Directory is: %s" % os.listdir(os.getcwd()))
os.removedirs(r'.\E\A')  # Delete A and then E
print("Directory is: %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, an OSError will be thrown.
import os

print("Directory is: %s" % os.listdir(os.getcwd()))
os.rename("test", "test2")
print("Rename succeeded.")
print("Directory is: %s" % os.listdir(os.getcwd()))
  • os.system(command) runs the system's shell commands (converting strings to commands)
import os

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

os.system('calc')  # Open calculator
  • os.curdir refers to the current directory (.)
  • os.pardir refers to the previous directory (..)
  • os.sep output OS specific path separator (in win, \ \, in Linux, /)
  • os.linesep line terminator used by the current platform (in win, in Linux)
  • os.name refers to the currently used 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 separately
  • os.path.dirname(path) removes the file name and returns the directory path separately
  • os.path.join(path1[, path2 [,...]]) combines the parts of path1 and path2 into a pathname
  • os.path.split(path) splits the file name and path, and returns a tuple of (f_path,f_name). If the directory is used completely, it will separate the last directory as a file name, and it will not judge 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

# Return file name
print(os.path.basename(r'C:\test\lsgo.txt'))  # lsgo.txt
# Return to directory path
print(os.path.dirname(r'C:\test\lsgo.txt'))  # C:\test
# Synthesize directory and filename into one path
print(os.path.join('C:\\', 'test', 'lsgo.txt'))  # C:\test\lsgo.txt
# Split file name and path
print(os.path.split(r'C:\test\lsgo.txt'))  # ('C:\\test', 'lsgo.txt')
# Separating file names from extensions
print(os.path.splitext(r'C:\test\lsgo.txt'))  # ('C:\\test\\lsgo', '.txt')
  • os.path.getsize(file) returns the specified file size in bytes.
  • os.path.getatime(file) returns the latest 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
  • Floating point seconds, which can be converted by 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 suspension point.
  • os.path.samefile(path1,path2) determines whether two paths, path1 and path2, point to the same file
import os

print(os.path.ismount('D:\\'))  # True
print(os.path.ismount('D:\\Test'))  # False

4. Serialization and deserialization

Python's pickle module implements basic data sequence and deserialization.

  • Through the serialization operation of pickle module, we can save the running object information in the program to a file for permanent storage.
  • Through the deserialization operation of pickle module, we can create the last saved object from the file.

The most commonly used functions in pickle module are:

pickle.dump(obj, file, [,protocol]) serializes obj objects into an already open file.

  • Obj: the obj object you want to serialize.
  • File: file name.
  • Protocol: the protocol used for serialization. If the item is omitted, it defaults to 0. If it is a negative value or high? Protocol, the highest protocol version is used.

pickle.load(file) serializes the objects in the file and reads them out.

  • 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'}}

# Using dump() to serialize data into a file
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()

# Use load() to serialize and read the data from the file
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'}}

summary

OK, so far, I've finished the introduction of file, file system and serialization and deserialization. You should have a lot of experience according to the above examples. Only deliberate practice can master a technology. There is no shortcut. Come on! See You!

reference:

  • https://www.runoob.com/python3/python3-tutorial.html
  • https://www.bilibili.com/video/av4050443

Related text:

Posted by AMCH on Sat, 26 Oct 2019 20:16:23 -0700