设为首页 加入收藏

TOP

python3--Linux文件自动更新小程序(一)
2017-10-13 10:36:44 】 浏览:3302
Tags:python3--Linux 文件 自动 更新 程序
  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 = "你需要更新的文件根目录"
 17 elif os.name == 'nt':
 18         ServerDir = "你需要更新的文件根目录"
 19         os.sep = '/'
 20 
 21 def print_point():
 22 
 23     for i in range(100):
 24         percent = 1.0*i/100*100
 25         print('完成进度:\033[1;31m %.2s %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 #用于读取更新文档目录的方法
 33 def read_update_dir(vision_flag):
 34     dirlist = []
 35     path_file_list1 = {}
 36     path_file_list2 = {}
 37 
 38     #定义需要更新的目录及文件列表
 39     #使用os.walk()函数实现遍历当前路径下的所有目录结构,输出为列表,列表值为tuple(元组)
 40     for i in os.walk("."):
 41         #print(dirlist)
 42         #将非空的目录及非顶层的目录保存到列表中
 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;31m 注意: \033[0mUpdateFile文件夹下不存在可更新文件!按回车键退出!")
 49         #print(dirlist)
 50         input("")
 51         exit()
 52 
 53     dirlist_length = len(dirlist)
 54     #print(dirlist_length)
 55     list_num = 0
 56     #使用循环取出需要更新的目录路径及文件名称列表
 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("需要更新的目录%d:\033[4;31m %s \033[0m"%((list_num+1),(ServerDir+update_dir+os.sep)))
 64 
 65         update_file = dirlist[list_num][-1]
 66         if vision_flag:
 67             print("需要更新的文件列表%d:%s"%((list_num+1),update_file))
 68         list_num+=1
 69         #这里使用了Python3中字典的特性,将每次循环取出来的字典,再update进一个新的字典,这样才能在字典中保存另一个字典
 70         path_file_list2.update(path_file_list1.fromkeys(update_file,(ServerDir+update_dir+os.sep)))
 71     return path_file_list2
 72 
 73 #用于显示已更新过的文档目录
 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("已更新的目录%d:\033[1;33m %s \033[0m"%((list_num2+1),(ServerDir+update_dir2)))
 87         update_file2 = dirlist2[list_num2][-1]
 88         print("已更新的文件列表%d:\033[1;31m %s \033[0m"%((list_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     #遍历字典path_file_list,取出其中的key,value用于显示和更新
100     for path_file,path_dir in path_file_list.items():
101         print(" 已更新:%s \033[1;33m %s \033[0m"%(("..."+path_dir[13::]),path_file))
102         time.sleep(0.5)
103 
104 def do_backup_file():
105     print_point()
106     print("\033[1;33m 备份服务器文件完成! \033[0m")
107 
108 #def into_server_need2update_file(need_update_path,need_update_file):
109     #pass
110 
111 def restart_server():
112     pass
113 
114 def main():
115     #update_flag用于表示文件是否已经更新过
116     update_flag = True
117     #vision_flag用于表示是否显示函数里的打印语句
118     vision_flag = True
119 
120     list_x = []
121 
122     #定义当前目录路径
123     forward_dir = get_foward_dir()
124 
125     #循环遍历当前文件夹,并将当前目录下的文件夹(
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linux中利用crontab创建计划任务,.. 下一篇入侵检测工具之RKHunter & AIDE

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目