File operations Open action
- Io.open (file, mode= ' R ', Buffering=-1, Encoding=none,errors=none, Newline=none, Closefd=true, Opener=none)
Returns a file object (Stream object) and a file descriptor. Failed to open file, return exception
- Basic use:
f = open("test")print(f.read())f.close()
Be sure to close it when you're done
- There are two types of file access modes:
Text mode and binary mode
The Open parameter file
- The name of the file to open or to create. If you do not specify a path, the default is the current path
Mode modes
Parameters |
Description |
R |
The default, which means read-only open, throws an exception if write is called. Throws a Filenotfounderror exception if the file does not exist |
W |
Write-only open, if the file does not exist, the file is created directly, and if the file exists, the contents of the file are emptied |
X |
Creates and writes a new file, throws a Fileexistserror exception if the file exists |
A |
Write open, append if file exists |
B |
Binary mode, independent of character encoding, byte operation using bytes type |
T |
The default, text mode, to understand the bytes of a file according to a character encoding, operation by character |
+ |
Read and write open a file. To the original read-only, write-only open to provide missing read or write ability, can not be used alone. The Get file object still follows the characteristics of R, W, A, X |
R Read Only, Wxa write only
Wxa can generate new files
File pointers
- Point to current byte position
- Mode=r pointer starts at 0
- Mode=a pointer starts at EOF
Tell ()
- Show the current position of the pointer
Seek (offset[, whence])
- Move the pointer position, offset moves the number of bytes,
- Whence text mode may have different representations of operations in binary mode
Text mode
- whence 0 Default value, indicating starting from scratch, offset can only be a positive integer
- Whence 1 indicates that offset accepts only 0 from the current position
- Whence 3 means starting with EOF, offset accepts only 0
- In binary mode
- whence 0 Default value, indicating starting from scratch, offset can only be a positive integer
- Whence 1 indicates that offset can be positive from the current position
- Whence 2 means that offset can be positively negative starting with EOF
Binary mode supports the offset of any starting point, starting from the beginning, from the tail, and from the middle position.
Backward seek can be hyper-bounded, but the forward seek, can not be super-bounded, otherwise throw an exception
Buffering: Buffer
- -1 indicates the default buffer size
- Binary mode: Use IO. Default_buffer_size Value Control
- Text mode: If it is an end device, use row cache, otherwise the same as binary mode
- 0 only used in binary mode, indicating off buffer
- 1 only used in text mode, indicating row buffering (flush if you see a newline character)
- Greater than 1 specifies the buffer size
Buffer buffers
- One memory space, FIFO queue, buffer full or threshold reached, flush to disk
Flush ()
- Write buffer data to disk
- When close () is called, Flush () is called first
Encoding
- Text-only mode use
- Default value of None using default encoding (Windows Gbk,linux UTF8)
Errors
- None and strict errors will throw an exception, ignore ignore
NewLine
- Default None ' \ n ', ' \ R ', ' \ r \ n ' are treated as newline (both converted to/n)
- "indicates that universal line breaks are not converted automatically
- Other legal characters indicate that the specified character is a line break
- Closefd
- Close the file descriptor, True to close, False to leave the descriptor after the file is closed
Read
- Read (Size=-1)
- Size read number of characters or bytes negative or none read to EOF
Line Read
ReadLine (Size=-1)
- Each time a row is read, size controls the number of characters or bytes that are read one line at a time
- ReadLines (Hint=-1)
Write
- Write (s) returns the number of characters written
- Writelines (lines) writes a list of strings
Other
- Seekable ()
- Readable ()
- Writable ()
- Closed
Context Management
Memory io Stringio
from io import StringIO
Bytesio
from io import BytesIO
File-like Object
- Class file object:
- sockets, stdin, StdOut are class file objects
Path operation
- We recommend using the Pathlib module, using the Path object to manipulate
from pathlib import Path
Path stitching and decomposition operators/
- Operator overloading
- Path Object/Path object
- Path object/String or string/path object
Parts
Output: ('/', ' data ', ' Mypythonobject ')
Joinpath
- Concatenate multiple characters into the path object
Get path
- STR Get path string
- Bytes Get path byte
Parent Directory
- Parent returns the Path class object
- Parents returns the parent directory to iterate over objects, index 0 is the current direct parent directory
Other
- The last part of the name directory
- The extension of the last section of the suffix directory
- Stem directory last section, no suffix
- Suffixes returns a list of multiple extension names
- With_suffix (suffix) supplement extension to path trailer, return new path, invalid extension presence
With_name (name) replaces the last part of the directory and returns a new path
- CWD () returns the current working directory
Home () return to the current home directory
- Is_dir () whether the directory
- Is_file () is normal file
- Is_symlink () whether soft link
- Is_socket () whether the socket file
- Is_block_device () whether the block device
- Is_char_device () whether the character device
Is_absolute () is the absolute path
- Resolve () returns the absolute path of the current path object and is parsed directly if it is a soft link
Absolute () returns an absolute path, it is recommended to use resolve
- Exists () whether the directory or file exists
- RmDir () Delete the empty directory. Does not provide a way to determine if the directory is empty
- Touch (mode=0o666,exist_ok=true) Create a file
As_uri () returns the path as a URI, such as "FILE:///ETC/PASSWD"
mkdir (Mode=0o777,parents=false,exist_ok=false)
- Parents whether the parent directory is created, True equals mkdir-p false, the parent directory does not exist, then the Filenotfounderror is thrown
- EXIST_OK False when the path exists and throws Fileexistserror true, this exception is ignored
Iterdir () Iteration current directory
Glob () pass with the given pattern
Rglob () passes a given pattern, and the recursive directory returns a generator
Match () pattern matching, successful return true
Stat () View file properties
- Lstat () with stat But if the link file is displayed, the file information itself
File operations
Open (mdoe= ' R ', Buffering=-1,encoding=none,errors=none,newline=none)
- Similar to the built-in function open returns a file object
Read_bytes ()
Read_text (Encoding=none,errors=none)
Path.write_bytes (data)
- Write_text (Data,encoding=none,errors=none)
OS Module
Os.name
- Windows is Nt,linux is POSIX
Os.uname ()
- Return System Information Linux only
Sys.platform
- WinDOS display Win32 Linux display Linux
Os.listdir (' 0:/temp ')
- Return to directory contents list
Os.stat (Path, *, Dir_fd=none, follow_symlinks=true)
- Essentially call the Linux stat
- Path: string or bytes of path, or FD file descriptor
- Follow_symlinks true returns the information for the file itself, False if the soft-link connection shows the soft links themselves
Os.chmod (path, mode, *, Dir_fd=none, follow_symlinks=true)
- Refer to the Linux chmod command
- Os.chown (path, UID, GID, *, Dir_fd=none, follow_symlinks=true)
- Refer to the Linux chown command
Shutil Module
Copyfileobj (FSRC, fdst[, length])
CopyFile (SRC, DST, *, follow_symlinks=true)
Copymode (SRC, DST, *, follow_symlinks=true)
Copystat (SRC, DST, *, follow_symlinks=true)
- Copy metadata, including permissions
Copy (SRC, DST, *, follow_symlinks=true)
- Copy content, permissions
Copy2 (SRC, DST, *, follow_symlinks=true)
Copytree (SRC, DST, Symlinks=false, Ignore=none, Copy_function=copy2, Ignore_dangling_symlinks=false)
- Recursive replication directory uses Copy2 by default
Rmtree (Path, Ignore_errors=false, Onerror=none)
- Recursive removal with caution
- Move (SRC, DST, copy_function=copy2)
- Move files, directories recursively
CSV file
If the field contains double quotes, commas, newline characters, you must enclose them in double quotation marks.
If the field itself contains double quotes, use two double quotes to indicate an escape
CSV module
INI file
[DEFAULT]a = test[mysql]default-character-set=utf8a = 1000[mysqld]datadir =/dbserver/dataport = 33060character-set-server=utf8sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
- The contents of the brackets are called section
- Each section is a key-value pair in key,value form, key is called option
Default section default must be uppercase
Configparser Module Configparserle Class
from configparser import ConfigParsercfg = ConfigParser()
A section can be treated as a key, and its corresponding value is a dictionary of key-value pairs that store option, that is, the INI file is a nested dictionary. Use an ordered dictionary by default
Read (filenames, Encoding=none)
- Read files, filenames can be a separate file or file list
Sections ()
- Returns the section list. Does not include the default section
Add_section (Section_name)
Has_section (Section_name)
Options (section)
- Returns the options for a section, including the options for the default section
Has_option (section)
- Is there a section option
Get (section, option, *, Raw=false, vars=none[, fallback])
- The option value from the specified section, if not found, go to default to find
- Getint (section, option, *, Raw=false, vars=none[, fallback])
- GetFloat (section, option, *, Raw=false, vars=none[, fallback])
Getboolean (section, option, *, Raw=false, vars=none[, fallback])
- Returns the specified type of data
Items (Raw=false, Vars=none)
- Returns all section names and their objects
Items (section, Raw=false, Vars=none)
- Returns the key value pair of option for the specified section two tuples
Set (section, option, value)
- Sets the option=value of the specified section (Option,value must be a string), the section does not exist and throws an exception
Remove_section (section)
- Remove section and its option
Remove_option (section, option)
- Removes the specified option from the specified section
- Write (FileObject, space_around_delimiters=true)
- Writes all the contents of the current config to FileObject
Serialization and deserialization Defined
Pickle Library
Dump (obj, Protocol=none, *, Fix_imports=true)
- Serializing an object to a bytes object
Dump (obj, file, Protocol=none, *, Fix_imports=true)
- Serializes an object to a file object, which is deposited into a file
Loads (file, *, Fix_imports=true, encoding= "ASCII", errors= "strict")
- Deserializing from a Bytes object
- Load (bytes_object, *, Fix_imports=true, encoding= "ASCII", errors= "strict")
- Read data deserialization from a file
Serialization and deserialization of experimental serialization applications
- Generally used in the network transport, the data is serialized and transmitted over the network to the remote node, the service on the remote server will receive the data deserialized use
- Note: The remote receive side must have a corresponding data type when deserializing, otherwise it will error. In particular, custom classes must have a consistent definition on the remote side
- Most projects are not stand-alone, single-service. The need to transfer data to other nodes over the network requires a lot of serialization and deserialization
- However, XML, Json, Protocol buffer, etc. are commonly used across platforms, across languages, and across protocols. Do not select pickle
JSON module
Common methods
import json
Dumps (obj, *, Skipkeys=false, ensure_ascii=true, Check_circular=true, Allow_nan=true, Cls=none, Indent=None, Separators=none, Default=none, Sort_keys=false, **kw)
Dump (obj, FP, *, Skipkeys=false, ensure_ascii=true, Check_circular=true, Allow_nan=true, Cls=none, Indent=none, Separators=none, Default=none, Sort_keys=false, **kw)
- JSON encoding and depositing files
Loads (s, *, Encoding=none, Cls=none, Object_hook=none, Parse_float=none, Parse_int=none, Parse_constant=none, object_ Pairs_hook=none, **kw)
- Load (FP, *, Cls=none, Object_hook=none, Parse_float=none, Parse_int=none, Parse_constant=none, Object_pairs_hook=none , **kw)
- JSON decoding, reading data from a file
Messagepack Module
- Similar to JSON, but the same data takes up less space than JSON
Installation
- $ pip Install Msgpack-python
Common methods
import msgpack
PACKB (o, **kwargs) <=> dumps
- Serializes the object. Provides dumps for compatibility with Pickle and JSON
UNPACKB (Packed, object_hook=none, List_hook=none, bool use_list=1, Encoding=none, unicode_errors= ' strict ', object_ Pairs_hook=none, Ext_hook=exttype, py_ssize_t max_str_len=2147483647, py_ssize_t max_bin_len=2147483647, Py_ssize_t max_array_len=2147483647, py_ssize_t max_map_len=2147483647, py_ssize_t max_ext_len=2147483647) <==> loads
- Deserializes the object. Provides loads to compatible
Pack (O, Stream, **kwargs) <==> dump
- The serialized object is saved to a file object. Provides a dump to be compatible with
- Unpack (stream, Object_hook=none, List_hook=none, bool use_list=1, Encoding=none, unicode_errors= ' strict ', object_ Pairs_hook=none, Ext_hook=exttype, py_ssize_t max_str_len=2147483647, py_ssize_t max_bin_len=2147483647, Py_ssize_t max_array_len=2147483647, py_ssize_t max_map_len=2147483647, py_ssize_t max_ext_len=2147483647) <==> load
- Deserializes the image to a file object. Provides load to be compatible with
Python Sixth Week Study notes (1)