ssh2.sftp_handle

SFTP handle, attributes and stat VFS classes.

class ssh2.sftp_handle.SFTPAttributes
atime
filesize
flags
gid
mtime
permissions
uid
class ssh2.sftp_handle.SFTPHandle
close(self)

Close handle. Called automatically when object is deleted and/or garbage collected.

Return type

int

fsetstat(self, SFTPAttributes attrs)

Set file handle attributes.

Parameters

attrs (ssh2.sftp.SFTPAttributes) – Attributes to set.

fstat(self)

Get file stat attributes from handle.

Return type

tuple(int, ssh2.sftp.SFTPAttributes)

fstat_ex(self, SFTPAttributes attrs, int setstat)

Get or set file attributes. Clients would typically use one of the fstat or fsetstat functions instead

fstatvfs(self)

Get file system statistics for handle

Return type

ssh2.sftp.SFTPStatVFS

fsync(self)

Sync file handle data.

Available from libssh2 >= 1.4.4

Return type

int

read(self, size_t buffer_maxlen=c_ssh2.LIBSSH2_CHANNEL_WINDOW_DEFAULT)

Read buffer from file handle.

Parameters

buffer_maxlen (int) – Max length of buffer to return.

Return type

bytes

readdir(self, size_t buffer_maxlen=1024)

Get directory listing from file handle, if any.

This function is a generator and should be iterated on.

File handle must be opened with ssh2.sftp.SFTP.readdir()

Parameters

buffer_maxlen – Max length of returned file entry.

Return type

iter(bytes)

readdir_ex(self, size_t longentry_maxlen=1024, size_t buffer_maxlen=1024)

Get directory listing from file handle, if any.

File handle must be opened with ssh2.sftp.SFTP.readdir()

This function is a generator and should be iterated on.

Parameters
  • buffer_maxlen – Max length of returned buffer.

  • longentry_maxlen – Max length of file list entry.

Return type

bytes

rewind(self)

Rewind file handle to beginning of file.

Return type

None

seek(self, size_t offset)

Deprecated, use seek64.

Seek file to given offset.

Parameters

offset (int) – Offset to seek to.

Return type

None

seek64(self, libssh2_uint64_t offset)

Seek file to given 64-bit offset.

Parameters

offset (int) – Offset to seek to.

Return type

None

tell(self)

Deprecated, use tell64.

Get current file handle offset.

Return type

int

tell64(self)

Get current file handle 64-bit offset.

Return type

int

write(self, bytes buf)

Write buffer to file handle.

Returns tuple of (error code, bytes written).

In blocking mode bytes_written will always equal len(buf) if no errors have occurred which would raise exception.

In non-blocking mode error_code can be LIBSSH2_ERROR_EAGAIN and bytes_written can be less than len(buf).

Clients should resume from that point on next call to write, ie buf[bytes_written_in_last_call:].

Parameters

buf (bytes) – Buffer to write.

Return type

tuple(int, int)

class ssh2.sftp_handle.SFTPStatVFS

File system statistics

f_bavail

Free blocks for non-root

f_bfree

Free blocks

f_blocks

Size of fs in f_frsize units

f_bsize

File system block size

f_favail

Free inodes for non-root

f_ffree

Free inodes

f_files

Inodes

f_flag

File system mount flags.

This property is a bit mask with defined bits LIBSSH2_SFTP_ST_RDONLY and LIBSSH2_SFTP_ST_NOSUID

f_frsize

Fragment size

f_fsid

File system ID

f_namemax

Maximum filename length