A "text file" opens and saves a wide variety of program detail analysis

Source: Internet
Author: User

Experiment target: 1) Provide a folder browse box, let the user select the text file to open, open and display the contents of the file.

2) When the user clicks the "OK" button, compared to whether the current file has been modified, if modified, the prompt "Overwrite save", "Discard Save" or "Save as" and realize its function.



Import Easygui as G

Import OS


File_path = G.fileopenbox (default= "e:/pathonaaa/a/")


With open (File_path) as Old_file:

#with as statement instead of try finally, with default close () function #

title = Os.path.basename (File_path)

Msg= the contents of "file"%s "are as follows:"% title

Text = Old_file.read ()

#这个是当前文件打开的内容, note that if there is binary content in the file, the Red Word error #

Text_after = G.textbox (Msg,title,text)

#如果当前文件在输出框里有变化, then the change is called Text_after, while the original Old-file file is closed in the background #


If text! = Text_after[:-1]:

#这个是整个程序的精华之处, use this statement to check if test has changed #

Choice = G.buttonbox ("Detected file content changes, please select the following actions:", "Warning", ("Overwrite save", "Discard Save", "Save As"))

if choice = = "Overwrite save":

With open (File_path, "w") as Old_file:

#以新写入的方式打开当前的文件 #

Old_file.write (Text_after)

#覆盖写入 #

if choice = = "Discard Save":

Pass

if choice = = "Save As":

Another_path = G.filesavebox (default= ". txt")

#先确定了新文件的路径 #

If Os.path.splitext (Another_path) [1]! = ". txt":

#如果新的文件没有保存成txt文件的话 #

Another_path + = ". txt"

#那就在屁股上加上. txt#

With open (Another_path, "w") as New_file:

New_file.write (Text_after)


============================ Split Line ==============================

If text! = Text_after[:-1]:

The key point of this sentence is that the read () output in the OS is a large string, so the large "string [:-1]" is just one less last character.

For example:

Aaa= "Aston flies over the planning bureau" #这是一个字符串 #

Aaa[:-1]= "Aston fly over the plan" #少了那个 "bureau" word #

So visible in the program text even if a word does not move is certainly not equal to text_after[:-1], but in the program really a word does not move but will not trigger "! = ", clearly one character missing, why or" = = "?

The reason is as follows: The Easygui.textbox function appends a line terminator ("\ n") behind the returned string, which means that a print will see one more of the back, and no print, but the simple string is not \ n. If we want to compare whether the string has changed, we need to manually ignore this. So "string [-1]" happens to be this \ n, here is an important detail.

This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1728566

A "text file" opens and saves a wide variety of program detail analysis

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.