1. 程式人生 > >文件分類本地存取

文件分類本地存取

folders local erro etc end val name ans failed

# coding=utf-8
from __future__ import print_function
import paramiko
import time
import os


# init a sftp connection
def sftp_connect(ip, port, usr, pw):
    t = paramiko.Transport(ip, port)
    t.connect(username=usr, password=pw)
    sftp = paramiko.SFTPClient.from_transport(t)
    return sftp


def writefile(ftxt, content): with open(ftxt, w) as f: f.write(content) def getdiferent(l1, l2): if l2 != l1: print (get different) #f = open("D:\Study\python\log\ppemote.txt", ‘r+‘) for x in l1: #f.write(x+"\n") if x in l2:
pass else: tmplist.append(x) #f.close() #f = open("D:\Study\python\log\local.txt", ‘r+‘) for y in l2: #f.write(y+"\n") if y in l1: pass else: tmplist.append(y) #f.close() else
: pass return tmplist def listdir(job_dir): dircontent = sftp1.listdir(job_dir) return dircontent def waiteseconds(sec): for x in range(sec, 0, -1): print (x) time.sleep(1) def classifylog(job_dir, list1, list2): print("remotelist and locallist is: ", len(list1), len(list2)) # compare 2 lists and get the different different_list = getdiferent(list1, list2) print("different_list is :", different_list, len(different_list)) if len(different_list) != 0: print ("list1 != list2") for j in range(0, len(different_list)): print ("---------------------------") casename = different_list[j] print ("case: ", casename) # new case folder os.chdir(localfolder + "\\" + job_dir.split(/job)[1]) localcase_list = os.listdir(localfolder + "\\" + job_dir.split(/job)[1]) if casename not in localcase_list: os.mkdir(casename) localfile = os.getcwd() + "\\" + casename + "\\" + "harness.log" # download log to local folder logdir = logfolder + "/" + casename try: sftp1.chdir(logdir) remotefile = sftp1.getcwd() + "/" + "harness.log" print ("remotefile: " + remotefile) sftp1.get(remotefile, localfile) print ("harness log download to localfile completed!") except IOError as e: print (e) print ("No log exists for this case:", casename) else: print ("list1 == list2") pass if __name__ == __main__: # init some vars tmplist = [] remote_ip = "x.x.x.x" sftp_port = 21 username = "username" password = "password" faileddir = "/home/username/job/failed" aborteddir = "/home/username/job/aborted" completedir = "/home/username/job/complete" localfolder = "D:\Study\python\log" logfolder = "/home/username/log" # new some folders in local disk os.chdir(localfolder) folder_list = os.listdir(localfolder) if "failed" not in folder_list: os.mkdir(failed) if "complete" not in folder_list: os.mkdir(complete) if "aborted" not in folder_list: os.mkdir(aborted) local_failed_dir = localfolder + "\\" + "failed" local_complete_dir = localfolder + "\\" + "complete" local_aborted_dir = localfolder + "\\" + "aborted" # new a sftp connection print ("sftp connecting ... please wait...") sftp1 = sftp_connect(remote_ip, sftp_port, username, password) print ("sftp connection completed") # check the failed/aborted/complete folders remote_failed_list = listdir(faileddir) for i in range(0, len(remote_failed_list)): casename1 = remote_failed_list[i].split(.xml)[0] remote_failed_list[i] = casename1 print ("remote_failed_list: ", len(remote_failed_list), remote_failed_list) remote_aborted_list = listdir(aborteddir) for i in range(0, len(remote_aborted_list)): casename2 = remote_aborted_list[i].split(.xml)[0] remote_aborted_list[i] = casename2 print ("remote_aborted_list: ", len(remote_aborted_list), remote_aborted_list) remote_complete_list = listdir(completedir) for i in range(0, len(remote_complete_list)): casename3 = remote_complete_list[i].split(.xml)[0] remote_complete_list[i] = casename3 print ("remote_complete_list: ", len(remote_complete_list), remote_complete_list) # check local failed/aborted/complete folders local_failed_list = os.listdir(local_failed_dir) print("local_failed_list: ", local_failed_list) local_aborted_list = os.listdir(local_aborted_dir) local_complete_list = os.listdir(local_complete_dir) # ==========classifylog========== print ("--------1 checking ‘failed‘------will start in 5 seconds------") waiteseconds(5) tmplist = [] classifylog(faileddir, remote_failed_list, local_failed_list) local_failed_list = remote_failed_list print ("failed log check completed") print ("--------2 checking ‘aborted‘-----will start in 5 seconds---------") waiteseconds(5) tmplist = [] classifylog(aborteddir, remote_aborted_list, local_aborted_list) local_aborted_list = remote_aborted_list print ("aborted log check completed") print ("--------3 checking ‘complete‘------will start in 5 seconds--------") waiteseconds(5) tmplist = [] classifylog(completedir, remote_complete_list, local_complete_list) local_complete_list = remote_complete_list print ("complete log check completed") print ("close sftp connection") sftp1.close()

資料:

文件讀寫:http://www.cnblogs.com/ymjyqsx/p/6554817.html

異常處理:http://www.cnblogs.com/Lival/p/6203111.html

os文件操作:http://www.jb51.net/article/59901.htm

文件分類本地存取