[英]Unzip a zip file on remote server using Python
I need to write Python code to login to remote server and navigate to Zip file path and then unzip & save on remote server.我需要编写 Python 代码来登录到远程服务器并导航到 Zip 文件路径,然后解压缩并保存在远程服务器上。 As a next step, I need to access the files inside the unzipped folder.下一步,我需要访问解压缩文件夹中的文件。 Can anyone please help.任何人都可以请帮忙。 I referred few links but unable to get complete solution.我提到了几个链接,但无法获得完整的解决方案。 https://medium.com/@keagileageek/paramiko-how-to-ssh-and-file-transfers-with-python-75766179de73 https://medium.com/@keagileageek/paramiko-how-to-ssh-and-file-transfers-with-python-75766179de73
First you can upload file using .put()
and next you can use external program unzip
to uncompress it.首先你可以使用.put()
上传文件,然后你可以使用外部程序unzip
来解压缩它。
Something similar to类似的东西
import paramiko
# login
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(hostname='192.168.0.1', username='user', password='PaSsWoRd')
# upload
ftp_client = ssh_client.open_sftp()
ftp_client.put('local.zip', 'remote.zip')
ftp_client.close()
# unzip
stdin, stdout, stderr = ssh_client.exec_command('unzip remote.zip')
print(stdout.read().decode())
# access files
stdin, stdout, stderr = ssh_client.exec_command('ls')
print(stdout.read().decode())
If you want to put in subfolder then you may need to create this subfolder如果要放入子文件夹,则可能需要创建此子文件夹
stdin, stdout, stderr = ssh_client.exec_command('mkdir upload')
ftp_client.put('local.zip', 'upload/remote.zip')
stdin, stdout, stderr = ssh_client.exec_command('cd upload ; unzip remote.zip')
stdin, stdout, stderr = ssh_client.exec_command('ls upload')
# or
stdin, stdout, stderr = ssh_client.exec_command('cd upload ; ls')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.