当前位置:首页 >> 脚本专栏

Python实现Windows和Linux之间互相传输文件(文件夹)的方法

项目中需要从Windows系统传输ISO文件到Linux测试系统,然后再Linux测试系统里安装这个ISO文件。所以就需要实现如何把文件从Windows系统传输到Linux系统中。

在项目中使用了pscp.exe这个工具,只要按照pscp.exe的使用说明操作即可。只要进入pscp.exe的安装位置,然后输入pscp即可查看pscp的使用说明。

下面是我机器上的:

Python实现Windows和Linux之间互相传输文件(文件夹)的方法

使用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) 

以上这篇Python实现Windows和Linux之间互相传输文件(文件夹)的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。