Python core Programming Chapter Nineth file part of the miscellaneous notes

Source: Internet
Author: User
Tags python script

Recently began to learn Python nineth file, do after-school questions encountered some problems, recorded for follow-up review or optimize error correction.

1. When writing a file, the time object is not known how to convert to a string to save the file, then read the file from the string to the object, later found can be converted with strftime and strptime2 functions.

A. Time DateTime object A can be converted to a string by A.strftime ("%y-%m-%d%h:%m:%s%f")

B. The obtained string A can be converted to a DateTime object by Datetime.datetime.strptime (A, formatted string (above)).

At the same time, we can use modules such as pickle to serialize and deserialize the contents of the file, simple processing, eliminating the details of the process, the module reads the storage file is binary mode, so W and R must add B. There is also a supposedly better module shelve, not yet known.

For example: with open (path, ' WB ') as F:

Pickle.dump (Custom structure object, F)

Here, dump serializes the structure object into a file, and there is a dumps it is used only for strings.

With open (path, ' RB ') as F:

Pickle.load (f)

Here the contents of the read file are deserialized to the original structure object, and the loads is also applied only to the string.

In addition, in open (file name, open mode) encountered a lot of Chinese and English path problems, reported abnormal can not be found, by converting the path string to Unicode after the resolution.

2. When matching file lines in some formats, the regular expression is applied. It is found here because in [] '-' there is a special meaning, so if you want to match '-' you need to put it to the front to avoid causing errors such as [-+*], while in brackets these special meaning symbols do not need to be preceded by ' \ ' to match itself.

When compiling regular compile (str), if there are multiple groupings () in Str, and some groupings match only those that do not need to be captured, you need to add them in parentheses. : such as (? : \d{1,3})

3. When the list is deleted, remove (element), when there are duplicate elements, can only delete an element, you can use set first to re-delete. In addition, it is best not to delete elements in the loop, causing the loop object to change easily, to copy an object deep, then loop the original object and manipulate the new object.

4.windows system, when using CMD to display the output of the Python script print, because the CMD software decoding mode is GBK, if the script is written in UTF-8, then the print output statement needs to be decoded from UTF-8 to Unicode and then encoded as GBK, To display correctly in CMD.

5. File Common way: Open multiple files can write: with open () as F1,open () as F2: Want to clear the contents of the file with F.truncate () OS.GETCWD () to get the current path.

Generally want to find a file, I like to use Os.listdir () and then stitching the path file, recursion until there is no directory in each path only files. This discovery misses the Os.walk () it will facilitate all directories to get a ternary tupple (Dirpath, Dirnames, filenames),

The first is the starting path, the second is the folder under the starting path, and the third is the file under the starting path.

Dirpath is a string that represents the path to the directory,

Dirnames is a list that contains the names of all subdirectories under Dirpath.

Filenames is a list that contains the name of a non-directory file.

These names do not contain path information, and you need to use Os.path.join (Dirpath, name) if you need to get the full path.

6.shutil module, shutil.copy () can directly copy a file or directory to another shutil.rmtree () can recursively delete a directory and all its contents.

Python core Programming Chapter Nineth file part of the miscellaneous notes

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.