Several considerations for Python file handling

Source: Internet
Author: User
Tags file handling

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)

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.