python怎么实现windows和linux之间互相传输文件|Python怎么实现Windows和Linux之间互相传输文件

更新时间:2021-07-01    来源:python    手机版     字体:

【www.bbyears.com--python】

项目中需要从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)

importos 

   

defWindow_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'<<<<<<<<<<<<<<<<<<<<<<<<<linux_file div="" style="word-wrap: break-word; color: rgb(0, 0, 0); font-family: " sans="" font-size:="" font-style:="" font-variant-ligatures:="" font-variant-caps:="" font-weight:="" letter-spacing:="" orphans:="" text-align:="" text-indent:="" text-transform:="" white-space:="" widows:="" word-spacing:="" -webkit-text-stroke-width:="" :="">>>>>>>>>>>>>>>>>>>>>>>>>Window_to_Linux_Dir begin' linux_file>

     

  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'<<<<<<<<<<<<<<<<<<<<<<<<<>>>>>>>>>>>>>>>>>>>>>>>>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_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'<<<<<<<<<<<<<<<<<<<<<<<<<

     

if__name__=='__main__': 

  password='*****' 

  window_path=r'D:' 

  username='****' 

  Linux_ip=ཆ.**.***.***' 

  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)

本文来源:http://www.bbyears.com/jiaocheng/127104.html