脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - Python实现Windows和Linux之间互相传输文件(文件夹)的方法

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

2020-10-07 22:16Python教程网 Python

下面小编就为大家带来一篇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)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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之间互相传输文件(文件夹)的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐