項目總結(四)----------Python實現Windows和Linux之間互相傳輸文件(文件夾)

項目中需要從Windows系統傳輸ISO文件到Linux測試系統,然後再Linux測試系統裏安裝這個ISO文件。所以就需要實現如何把文件從Windows系統傳輸到Linux系統中。


在項目中使用了pscp.exe這個工具,只要按照pscp.exe的使用說明操作即可。只要進入pscp.exe的安裝位置,然後輸入pscp即可查看pscp的使用說明。

下面是我機器上的:



使用Python實現也挺簡單的,下面的code主要介紹4中情況:

1. windows傳輸文件到Linux

2. windows傳輸文件夾到Linux

3. Linux傳輸文件到windows

4. Linux傳輸文件夾到windows


code如下:(運行環境:python27+eclipse+pydev

import os


def Window_to_Linux_File(window_path, Linux_path, Linux_ip, username, password):
        print '>>>>>>>>>>>>>>>>>>>>>>>>>Window_to_Linux_File  begin'
    
        cmd='C:\STAF\lib\python\SBS\esxtest\pscp.exe -pw {password} {window_path} {username}@{Linux_ip}:{Linux_path}'.format(
                            password=password, window_path=window_path, username=username, Linux_ip=Linux_ip, Linux_path=Linux_path)
        os.system(cmd)
        
        print '<<<<<<<<<<<<<<<<<<<<<<<<<<Window_to_Linux_File end'
        
        
def Window_to_Linux_Dir(window_path, Linux_path, Linux_ip, username, password):
    print '>>>>>>>>>>>>>>>>>>>>>>>>>Window_to_Linux_Dir  begin'
    
    cmd='C:\STAF\lib\python\SBS\esxtest\pscp.exe -pw {password} -r {window_path} {username}@{Linux_ip}:{Linux_path}'.format(
                            password=password, window_path=window_path, username=username,Linux_ip=Linux_ip, Linux_path=Linux_path)
    os.system(cmd )
    
    print '<<<<<<<<<<<<<<<<<<<<<<<<<<Window_to_Linux_Dir end'
    
    
def Linux_to_Window_File(Linux_path, window_path, Linux_ip, username, password):
    print '>>>>>>>>>>>>>>>>>>>>>>>>>Linux_to_Window_File  begin'
    
    cmd='C:\STAF\lib\python\SBS\esxtest\pscp.exe -pw {password} {username}@{Linux_ip}:{Linux_path} {window_path}'.format(
                            password=password, username=username,Linux_ip=Linux_ip, Linux_path=Linux_path, window_path=window_path)
    os.system(cmd )
    
    print '<<<<<<<<<<<<<<<<<<<<<<<<<<Linux_to_Window_File end'   
     
    
def Linux_to_Window_Dir(Linux_path, window_path, Linux_ip, username, password):
    print '>>>>>>>>>>>>>>>>>>>>>>>>>Linux_to_Window_Dir  begin'
    
    cmd='C:\STAF\lib\python\SBS\esxtest\pscp.exe -pw {password} -r {username}@{Linux_ip}:{Linux_path} {window_path}'.format(
                            password=password, username=username,Linux_ip=Linux_ip, Linux_path=Linux_path, window_path=window_path)
    os.system(cmd)
    
    print '<<<<<<<<<<<<<<<<<<<<<<<<<<Linux_to_Window_Dir end'
    
    

if __name__ == '__main__':
    password='*****'
    window_path=r'D:'
    username='****'
    Linux_ip='10.**.***.***'
    Linux_path=r'/var/backup'
    
    Window_to_Linux_File(window_path, Linux_path, Linux_ip, username, password)
    #Window_to_Linux_Dir(window_path, Linux_path, Linux_ip, username, password)
    #Linux_to_Window_File(Linux_path, window_path, Linux_ip, username, password))
    #Linux_to_Window_Dir(Linux_path, window_path, Linux_ip, username, password)













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章