Python3 -- automatically update Applets in Linux Files,

Source: Internet
Author: User

Python3 -- automatically update Applets in Linux Files,

1 #! /Usr/bin/python3 2 # coding = UTF-8 3 4 # author = warlock921 5 6 import OS 7 import sys 8 import time 9 import paramiko 10 11 ServerDir = ''12 ServerUpdateFileDir =' updateFile '13 ServerBakDir = "BackupFile" 14 15 if OS. name = 'posix': 16 ServerDir = "the root directory of the file you want to update" 17 elif OS. name = 'nt ': 18 ServerDir = "root directory of the file to be updated" 19 OS. sep = '/' 20 21 def print_point (): 22 23 for I in range (100): 24 percent = 1. 0 * I/100*100 25 print ('Progress: \ 033 [1; 31 m %. 2 s % s \ 033 [0m' % (str (percent), '%'), end = '\ R') 26 time. sleep (0.08) 27 28 29 def get_foward_dir (): 30 return OS. getcwd () 31 32 # method for reading the updated document directory 33 def read_update_dir (vision_flag ): 34 dirlist = [] 35 path_file_list1 = {} 36 path_file_list2 = {} 37 38 # define the Directory and file list to be updated 39 # Use OS. the walk () function traverses all directory structures in the current path and outputs the list. The list value is tuple (tuples) 40 for I in OS. walk (". "): 41 # print (dirlist) 42 # Save non-empty directories and non-top-level directories to the list 43 if I [-1]! = [] And I [0]! = ". ": 44 dirlist. append (I) 45 # print (dirlist) 46 if dirlist = []: 47 if vision_flag: 48 print ("\ 033 [1; 31 m note: \ 033 [No updatable file exists in the 0mUpdateFile folder! Press enter to exit! ") 49 # print (dirlist) 50 input (" ") 51 exit () 52 53 dirlist_length = len (dirlist) 54 # print (dirlist_length) 55 list_num = 0 56 # use a loop to retrieve the directory path and file name list to be updated 57 while list_num <dirlist_length: 58 59 update_dir = dirlist [list_num] [0] [1:] 60 61 if vision_flag: 62 print ('-' * 50) 63 print ("directory to be updated % d: \ 033 [4; 31 m % s \ 033 [0 m "% (list_num + 1), (ServerDir + update_dir + OS. sep) 64 65 update_file = dirlist [list_num] [-1] 66 if vision_flag: 67 print ("list of files to be updated % d: % s" % (list_num + 1), update_file )) 68 list_num + = 1 69 # Here we use the dictionary feature in Python3 to update the dictionary obtained from each loop into a new dictionary, in this way, another dictionary 70 path_file_list2.update (path_file_list1.fromkeys (update_file, (ServerDir + update_dir + OS. sep) 71 return path_file_list2 72 73 # used to display the updated document directory 74 def read_updated_dir (): 75 dirlist2 = [] 76 for j in OS. walk (". "): 77 if j [-1]! = [] And j [0]! = ". ": 78 dirlist2.append (j) 79 # print (dirlist2) 80 dirlist2_length = len (dirlist2) 81 # print (dirlist_length) 82 list_num2 = 0 83 while list_num2 <dirlist2_length: 84 update_dir2 = dirlist2 [list_num2] [0] [1:] 85 print ('-' * 50) 86 print ("updated directory % d: \ 033 [1; 33 m % s \ 033 [0 m "% (list_num2 + 1), (ServerDir + update_dir2 ))) 87 update_file2 = dirlist2 [list_num2] [-1] 88 print ("updated file list % d: \ 033 [1; 31 m % s \ 033 [0 m "% (lis T_num2 + 1), update_file2) 89 list_num2 + = 1 90 91 92 def read_bak_dir (): 93 pass 94 95 def do_update_file (): 96 vision_flag = False 97 path_file_list = read_update_dir (vision_flag) 98 # pfl_len = len (path_file_list) 99 # traverse the dictionary path_file_list and retrieve the key. value is used to display and update 100 for path_file, path_dir in path_file_list.items (): 101 print ("Updated: % s \ 033 [1; 33 m % s \ 033 [0 m" % (("... "+ path_dir [13:]), path_file) 102 time. sleep (0.5) 103 104 def do_backup_file (): 105 print_point () 106 print ("\ 033 [1; 33 m Backup Server File completed! \ 033 [0 m ") 107 108 # def pai_server_need2update_file (need_update_path, need_update_file): 109 # pass110 111 def restart_server (): 112 pass113 114 def main (): 115 # update_flag indicates whether the file has been updated 116 update_flag = True117 # vision_flag indicates whether to display the print statement 118 vision_flag = True119 120 list_x = [] 121 122 # define the current directory path 123 forward_dir = get_foward_dir () 124 125 # traverse the current folder cyclically and press the folder (excluding files) in the current directory to the list 126 for x in OS. listdir (): 127 if OS. path. isdir (x): 128 list_x.append (x) 129 130 # determine whether an UpdateFile file exists 131 if ServerUpdateFileDir in list_x: 132 # print ("hit \ 033 [1; 31 m % s \ 033 [0 m folder! "% ServerUpdateFileDir) 133 # change the working directory and enter the 134 OS folder to be updated. chdir ((". "+ OS. sep + ServerUpdateFileDir) 135 else: 136 # If the UpdateFile file does not exist, ask the user to choose whether to create the 137 print ("\ 033 [1; 31 m UpdateFile Folder does not exist, create the UpdateFile folder first, otherwise it cannot be updated! \ 033 [0 m ") 138 mkdir_input = input (" do you want to create an UpdateFile folder? (Y/n): ") 139 if mkdir_input = 'y' or mkdir_input = 'y': 140 OS. system ('mkdir updatefile') 141 print ("The UpdateFile folder has been created! ") 142 OS. chdir ((". "+ OS. sep + ServerUpdateFileDir) 143 input ('') 144 elif mkdir_input = 'n' or mkdir_input = 'N': 145 print (" The UpdateFile folder is not created and cannot be updated, system exited! ") 146 input ('') 147 exit () 148 else: 149 pass150 151 # Start loop, update program main program 152 while True: 153 154 OS. system ('clear ') 155 print ("**************** update program v1.01 ***************** ") 156 # font print color change Syntax: \ 033 [1; 33; 40 m % s \ 033 [0m157 print ("hit \ 033 [1; 31 m % s \ 033 [0 m folder! "% ServerUpdateFileDir) 158 print (" current working directory: \ 033 [1; 33; 40 m % s \ 033 [0 m ") % forward_dir) 159 print ("current working folder: % s" % get_foward_dir () 160 161 # determine if 162 has been updated if update_flag: 163 # if not updated, run the following statement 164 read_update_dir (vision_flag) 165 print ('* 50) 166 update_usr_input = input ("\ 033 [1; 31 m note \ 033 [0 m whether to start updating the file: \ n 1. start updating \ n 2. only backup file \ n 3 is not updated. to exit the update program \ n, select (number key 1/2/3, default 3): ") 167 if update_usr_input =" 1 ": 168 print (" \ 033 [1; update starts at 31 mb. Do not Other operations... \ 033 [0 m ") 169 170 # Back up 171 do_backup_file () 172 # And then update 173 do_update_file () 174 175 print (" Update successful! ") 176 update_flag = False177 input (" ") 178 continue179 elif update_usr_input =" 2 ": 180 do_backup_file () 181 input ("") 182 continue183 elif update_usr_input = "3": 184 OS. system ('clear') 185 print ("the system exits! ") 186 exit () 187 else: 188 OS. system ('clear') 189 print (" the system exits! ") 190 exit () 191 else: 192 # Once updated, run the following code 193 print ('* 50) 194 update_usr_input = input (" \ 033 [1; 31 m Note: \ 033 [0 m website has been updated: \ n 1. view the updated directory \ n 2. exit update program \ n select (number key 1/2, default 2): ") 195 if update_usr_input =" 1 ": 196 read_updated_dir () 197 input ("") 198 continue199 elif update_usr_input = "2": 200 OS. system ('clear') 201 print ("the system exits! ") 202 exit () 203 else: 204 OS. system ('clear') 205 print (" the system exits! ") 206 exit () 207 208 209 if _ name _ = '_ main _': 210 main ()

 

Related Article

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.