簡體   English   中英

使用 Python 在遠程服務器上解壓縮 zip 文件

[英]Unzip a zip file on remote server using Python

我需要編寫 Python 代碼來登錄到遠程服務器並導航到 Zip 文件路徑,然后解壓縮並保存在遠程服務器上。 下一步,我需要訪問解壓縮文件夾中的文件。 任何人都可以請幫忙。 我提到了幾個鏈接,但無法獲得完整的解決方案。 https://medium.com/@keagileageek/paramiko-how-to-ssh-and-file-transfers-with-python-75766179de73

首先你可以使用.put()上傳文件,然后你可以使用外部程序unzip來解壓縮它。

類似的東西

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())

如果要放入子文件夾,則可能需要創建此子文件夾

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM