Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Python implements automatic backup of reading, rewriting, overwriting and rewriting of operating documents.

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

# coding:utf-8import time # time formatting and operation of backup files suspended using import shutil # as file backup # description of requirements # requirements: # 1, document path name can be entered; # 2, text wrapping input; # 3, text auto-save; # 4, readable text # 5, override rewrite # 6. If you overwrite, back up the previous files. " # define the function def read_text (text_url): with open (text_url, "r") as read: for i in read.readlines (): print i print "*" * 20, "end of document content display" Def continue_write (text_url): write_flag = 1 read_flag = 1 while write_flag = = 1: print "Please enter what you need to enter:\ n" get_new_string = raw_input () with open (text_url) "a +") as write: write.write ("\ n" + get_new_string) # wrap each time you enter while read_flag = = 1: print "the value you entered has been saved And choose whether to print to view? \ nAnykey "print" means to view! \ nenter Q or Q but do not view it, continue to enter! \ n "get_search_input_key = raw_input () if get_search_input_key = =" Q "or get_search_input_key = =" Q ": continue_write (text_url) read_flag = 0 else: read_text (text_url) read_flag = 0 print" if you confirm to exit editing Please enter Q or Q to exit! \ n "\" any key, it means to accept the continued input! \ n "get_input_key = raw_input () if get_input_key = =" Q "or get_input_key = =" Q ": print" GoodBye... " Write_flag = 0 else: read_flag = 1 # can again determine whether read operation continue # continue circular write operation # define rewrite document function def cover_write (text_url): flag_write = 1 print "is backing up the original file, backing up the file with system time and .bak as backup file! \ n "# reference time module to format system time # shutil.copy the first parameter is the original file path, and the second parameter is the backup file path shutil.copy (text_url, text_url +". "+ time.strftime ("% Y%m%d%H%M%S ") +" .bak ") print" Please wait 2 seconds. As the backup time of the original file.\ n "time.sleep (2) print" now you can rewrite your file.\ n "print" Please enter what you want to rewrite.\ n "get_input_string = raw_input () with open (text_url) "w") as cover: cover.write (get_input_string) while flag_write = = 1: print "would you like to continue to enter? \ n "\" any key will continue to enter, enter Q or Q to exit the input! " Get_write_continue_key = raw_input () if get_write_continue_key = = "Q" or get_write_continue_key = = "Q": flag_write = 0 else: continue_write (text_url) flag_write = 0 print "currently your document content is:\ n" read_text (text_url) # system main program entry if _ _ name__ = "_ _ main__": print r "Please enter the path and name of the document you want to read." Print r "for example: e:\ Users\ Administrator\ Desktop\ test.txt" print "Please enter:\ n" text_url = raw_input () print "\ n" print "*" * 20, "document content display begins" "*" * 20 # call the document view function read_text (text_url) # print spaces to separate print'\ n'# print 50 spaces from the document content * Also for the delimiter to use print'- * -'* 10, "enter the main program", "- * -" * 10 print "need to continue to add new content? \ n "\" any key means to continue entering\ n "\" enter quit, then quit! \ n "\" enter cover, then overwrite the full text and rewrite! The system will automatically back up old documents, please proceed with caution! " Is_continue_or_cover = raw_input () if is_continue_or_cover = = "quit": print "GoodBye..." Elif is_continue_or_cover = "cover": cover_write (text_url) else: continue_write (text_url)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report