Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #82501 > unrolled thread
| Started by | "水静流深" <1248283536@qq.com> |
|---|---|
| First post | 2014-12-14 20:49 +0800 |
| Last post | 2014-12-14 10:58 -0500 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
how to delete my file if exits in the remote server with paramiko? "水静流深" <1248283536@qq.com> - 2014-12-14 20:49 +0800
Re: how to delete my file if exits in the remote server with paramiko? Roy Smith <roy@panix.com> - 2014-12-14 10:58 -0500
| From | "水静流深" <1248283536@qq.com> |
|---|---|
| Date | 2014-12-14 20:49 +0800 |
| Subject | how to delete my file if exits in the remote server with paramiko? |
| Message-ID | <mailman.16944.1418561416.18130.python-list@python.org> |
[Multipart message — attachments visible in raw view] — view raw
I want to delete the file "names.txt" if it exits in "/home/names.txt" in my remote vps server.
import paramiko
host = "vps ip"
port = 22
transport = paramiko.Transport((host, port))
password = "key"
username = "root"
transport.connect(username = username, password = password)
sftp = paramiko.SFTPClient.from_transport(transport)
if "names.txt" in sftp.listdir("/home") : sftp.remove("/home/names.txt")
Is there more elegant way to do the same work?
[toc] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-12-14 10:58 -0500 |
| Message-ID | <roy-0F10F8.10582514122014@news.panix.com> |
| In reply to | #82501 |
In article <mailman.16944.1418561416.18130.python-list@python.org>,
"????" <1248283536@qq.com> wrote:
> I want to delete the file "names.txt" if it exits in "/home/names.txt" in my
> remote vps server.
> import paramiko
> host = "vps ip"
> port = 22
> transport = paramiko.Transport((host, port))
> password = "key"
> username = "root"
> transport.connect(username = username, password = password)
> sftp = paramiko.SFTPClient.from_transport(transport)
> if "names.txt" in sftp.listdir("/home") : sftp.remove("/home/names.txt")
>
>
> Is there more elegant way to do the same work?
I generally try to avoid using raw paramiko. Fabric gives you a much
nicer interface to the same basic functionality (and is indeed layered
on top of paramiko). The above functionality can be expressed in fabric
as simply as:
---------------------------------------------
from fabric.api import env, run
env.host_string = "my-remote-hostname"
run("rm -f /tmp/roy-temp")
---------------------------------------------
Of course, I cheated a little bit. I'm assuming you have ssh access to
your remote host; the above code depends on that. Going through sftp
makes everything more difficult. You can drive sftp through fabric, but
it's a lot more painful. I also assume that you can use ssh key
authentication, which simplifies everything compared to passing
usernames and passwords around.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web