0

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

我想我可以使用 chilkat sftp sftp = 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.")

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

4

1 回答 1

0

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

虽然一些 SFTP 客户端(客户端库)允许在本地模拟“当前工作目录”,但 Chilkat 似乎并非如此。

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

要创建文件,请使用OpenFile方法。要写入其内容,请使用其中一种WriteFile*方法。

于 2015-01-05T09:32:38.163 回答