File processing is a common operation in programming, file opening, closing, renaming, deleting, appending, copying, random reading and writing are very easy to understand and use. It is important to note that the file is closed securely, with the use statement easy and convenient:
With open (pathname, "R") as MyFile:
Do_some_with (myfile)
File processing for CSV
CSV modules work well with CSV files, while pandas modules can handle large CSV files, handle HTML and more, and provide chunked processing.
File processing of XML
For smaller XML files, it is best to use celementtree, at least with ElementTree, and for large files, preferably with lxml.
Serialization and serialization of file content
Using pickle to serialize and deserialize very simple dump () and load (), but note that Pickle can not achieve atomic operation, data source sensitive, there is a security risk. Another form of serialization is JSON. The same is dumps (), load (), good extensibility, can specify decoder, performance is worse than pickle.
Log file Processing
It is important to note that logging is thread-safe to log files using the logging module, and that multiple processes are not written to the same log at the same time.
Image file Processing
For the usual file image processing, the PIL module is sufficient. In the Linux environment to pay attention to the relevant Image library installation, the general ImageMagick Library is preferred. If it involves image recognition and other deep-seated image processing, it is necessary to seek OPENCV's help.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Several considerations for Python file handling