How to copy file from Server to Mac through python?

时间:2016-12-02 04:51:00

标签: python macos server copy

i want to copy file from the following path to my local machine mac but i am un able to do so it gives error that this is not a directory \builds\prgbuilds-readonly\MacIOS\DgnClientSdk i am using the following script in mac

import ftplib from urlparse import urlparse import ftputil import sys import os import socket import shutil import pickle import subprocess

return_list = []

def VerifySharedPath(path):    
if not os.path.exists(path):        
    print path,"does not exists"
    sys.exit(1)

def Download_From_Shared_Path(srcPath,destPath,buildNo,FileName,subPath,language_prefix): VerifySharedPath(srcPath)
dirList = os.listdir(srcPath)
Lasttested_build_no='' #find latest build directory using highest number in the directory list LATEST = buildNo if language_prefix == None: language_prefix = '' for dir in dirList: if buildNo in dir and language_prefix in dir: if LATEST < dir: LATEST = dir print '***LATEST build is '+ LATEST

SRC_PATH = os.path.join(srcPath,LATEST)

VerifySharedPath(SRC_PATH)    

DEST_PATH = os.path.join(destPath,LATEST)

if subPath != None: #subpath is given, join it with source path and verify 
    SRC_PATH = os.path.join(SRC_PATH,subPath)        
    VerifySharedPath(SRC_PATH)
print 'LATEST build will be downloaded from ',SRC_PATH
#print os.listdir(SRC_PATH)
if not os.path.exists(DEST_PATH):   #latest is build is not downloaded yet
    if FileName == '':     #check whether source path is a file or directory then copy it through respective functions.
        try:
            shutil.copytree(SRC_PATH,DEST_PATH)               
        except shutil.Error as e:
             print('Error: %s' % e)
             return_list.insert(0,"Fail")
             return_list.insert(1,LATEST)
             return return_list
        except IOError as e:
              print('Error: %s' % e.strerror)
              return_list.insert(0,"Fail")
              return_list.insert(1,LATEST)
              return return_list
    else:            
       try:         
           if SRC_PATH.endswith(FileName):
              DEST_PATH=DEST_PATH.strip(FileName)
              os.mkdir(DEST_PATH)
              shutil.copy(SRC_PATH,DEST_PATH)
           else:
            os.mkdir(DEST_PATH) 
            files = os.listdir(SRC_PATH)  
            files =[i for i in files if FileName in i]
            print files
            if len(files) >0 :
                #FileName = FileName.strip('*')
                for file in files:
                    FileName = file
                    FILE_PATH = os.path.join(DEST_PATH,FileName)
                    SRC_PATH_tem = os.path.join(SRC_PATH,FileName)     
                    print '*** Downloading "%s" Started: Please wait....'%FileName           
                    shutil.copy2(SRC_PATH_tem,FILE_PATH)        
            else:
             return_list.insert(0,"Fail")
             return_list.insert(1,LATEST)
             return return_list
       except shutil.Error as e:
            print('Error: %s' % e)
            return_list.insert(0,"Fail")
            return_list.insert(1,LATEST)
            return return_list
       except IOError as e:
            print('Error: %s' % e.strerror)
            return_list.insert(0,"Fail")
            return_list.insert(1,LATEST)
            return return_list
    print '*** Latest build is avaialable at %s ' % DEST_PATH
    print '*** Build is there. Happy Testing!'
    return_list.insert(0, "Pass")
    return_list.insert(1, DEST_PATH)
    return return_list
else:
    print "LATEST BUILD IS ALREADY DOWNLOADED"
    return_list.insert(0,"LATEST BUILD IS ALREADY DOWNLOADED")
    return_list.insert(1,LATEST)
    return return_list

0 个答案:

没有答案