使用Chilkat sftppython库在远程上更改目录并创建一个新文件

2024-09-27 00:11:28 发布

您现在位置:Python中文网/ 问答频道 /正文

http://www.example-code.com/python/sftp_writeTextFile.asp

我想我可以使用chilkat sftpsftp = chilkat.CkSFtp()30天试用版登录系统。在

现在我在根目录下(在远程机器中),有两个文件夹。我想更改这两个文件夹中的一个并在那里创建一个txt文件。在

我该怎么做

import sys
import chilkat

sftp = chilkat.CkSFtp()

success = sftp.UnlockComponent("Anything for 30-day trial")
if (success != True):
    print(sftp.lastErrorText())
    sys.exit()

#  Set some timeouts, in milliseconds:
sftp.put_ConnectTimeoutMs(15000)
sftp.put_IdleTimeoutMs(15000)

#  Connect to the SSH server.
#  The standard SSH port = 22
#  The hostname may be a hostname or IP address.

port = 22
success = sftp.Connect(hostname,port)
if (success != True):
    print(sftp.lastErrorText())
    sys.exit()

#  Authenticate with the SSH server.  Chilkat SFTP supports
#  both password-based authenication as well as public-key
#  authentication.  This example uses password authenication.
success = sftp.AuthenticatePw(username, password)
if (success != True):
    print(sftp.lastErrorText())
    sys.exit()

print("Success.")

此脚本成功执行并打印“成功”


Tags: trueifportexamplesysexitpasswordssh
1条回答
网友
1楼 · 发布于 2024-09-27 00:11:28

SFTP协议没有“当前工作目录”的概念(与FTP相反)。在

虽然一些SFTP客户机(客户机库)允许在本地模拟“当前工作目录”,但Chilkat似乎不是这样。在

所以在创建文件时必须使用绝对路径,例如/folder1/file.txt

要创建文件,请使用^{}方法。要编写其内容,请使用WriteFile*方法之一。在

相关问题 更多 >

    热门问题