Package paramiko :: Class SFTPClient
[frames] | no frames]

Class SFTPClient

source code

object --+    
         |    
  BaseSFTP --+
             |
            SFTPClient
Known Subclasses:

SFTP client object. SFTPClient is used to open an sftp session across an open ssh Transport and do remote file operations.

Instance Methods
 
__init__(self, sock)
Create an SFTP client from an existing Channel.
source code
 
chdir(self, path)
Change the "current directory" of this SFTP session.
source code
 
chmod(self, path, mode)
Change the mode (permissions) of a file.
source code
 
chown(self, path, uid, gid)
Change the owner (uid) and group (gid) of a file.
source code
 
close(self)
Close the SFTP session and its underlying channel.
source code
SFTPFile
file(self, filename, mode='r', bufsize=-1)
Open a file on the remote server.
source code
 
get(self, remotepath, localpath, callback=None)
Copy a remote file (remotepath) from the SFTP server to the local host as localpath.
source code
Channel
get_channel(self)
Return the underlying Channel object for this SFTP session.
source code
str
getcwd(self)
Return the "current working directory" for this SFTP session, as emulated by paramiko.
source code
list of str
listdir(self, path='.')
Return a list containing the names of the entries in the given path.
source code
list of SFTPAttributes
listdir_attr(self, path='.')
Return a list containing SFTPAttributes objects corresponding to files in the given path.
source code
SFTPAttributes
lstat(self, path)
Retrieve information about a file on the remote system, without following symbolic links (shortcuts).
source code
 
mkdir(self, path, mode=511)
Create a folder (directory) named path with numeric mode mode.
source code
str
normalize(self, path)
Return the normalized path (on the server) of a given path.
source code
SFTPFile
open(self, filename, mode='r', bufsize=-1)
Open a file on the remote server.
source code
SFTPAttributes
put(self, localpath, remotepath, callback=None)
Copy a local file (localpath) to the SFTP server as remotepath.
source code
str
readlink(self, path)
Return the target of a symbolic link (shortcut).
source code
 
remove(self, path)
Remove the file at the given path.
source code
 
rename(self, oldpath, newpath)
Rename a file or folder from oldpath to newpath.
source code
 
rmdir(self, path)
Remove the folder named path.
source code
SFTPAttributes
stat(self, path)
Retrieve information about a file on the remote system.
source code
 
symlink(self, source, dest)
Create a symbolic link (shortcut) of the source path at destination.
source code
 
truncate(self, path, size)
Change the size of the file specified by path.
source code
 
unlink(self, path)
Remove the file at the given path.
source code
 
utime(self, path, times)
Set the access and modified times of the file specified by path.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods
SFTPClient
from_transport(cls, t)
Create an SFTP client channel from an open Transport.
source code
Properties

Inherited from object: __class__

Method Details

__init__(self, sock)
(Constructor)

source code 

Create an SFTP client from an existing Channel. The channel should already have requested the "sftp" subsystem.

An alternate way to create an SFTP client context is by using from_transport.

Parameters:
Raises:
  • SSHException - if there's an exception while negotiating sftp
Overrides: object.__init__

chdir(self, path)

source code 

Change the "current directory" of this SFTP session. Since SFTP doesn't really have the concept of a current working directory, this is emulated by paramiko. Once you use this method to set a working directory, all operations on this SFTPClient object will be relative to that path. You can pass in None to stop using a current working directory.

Parameters:
  • path (str) - new current working directory
Raises:
  • IOError - if the requested path doesn't exist on the server

Since: 1.4

chmod(self, path, mode)

source code 

Change the mode (permissions) of a file. The permissions are unix-style and identical to those used by python's os.chmod function.

Parameters:
  • path (str) - path of the file to change the permissions of
  • mode (int) - new permissions

chown(self, path, uid, gid)

source code 

Change the owner (uid) and group (gid) of a file. As with python's os.chown function, you must pass both arguments, so if you only want to change one, use stat first to retrieve the current owner and group.

Parameters:
  • path (str) - path of the file to change the owner and group of
  • uid (int) - new owner's uid
  • gid (int) - new group id

close(self)

source code 

Close the SFTP session and its underlying channel.

Since: 1.4

file(self, filename, mode='r', bufsize=-1)

source code 

Open a file on the remote server. The arguments are the same as for python's built-in file (aka open). A file-like object is returned, which closely mimics the behavior of a normal python file object.

The mode indicates how the file is to be opened: 'r' for reading, 'w' for writing (truncating an existing file), 'a' for appending, 'r+' for reading/writing, 'w+' for reading/writing (truncating an existing file), 'a+' for reading/appending. The python 'b' flag is ignored, since SSH treats all files as binary. The 'U' flag is supported in a compatible way.

Since 1.5.2, an 'x' flag indicates that the operation should only succeed if the file was created and did not previously exist. This has no direct mapping to python's file flags, but is commonly known as the O_EXCL flag in posix.

The file will be buffered in standard python style by default, but can be altered with the bufsize parameter. 0 turns off buffering, 1 uses line buffering, and any number greater than 1 (>1) uses that specific buffer size.

Parameters:
  • filename (str) - name of the file to open
  • mode (str) - mode (python-style) to open in
  • bufsize (int) - desired buffering (-1 = default buffer size)
Returns: SFTPFile
a file object representing the open file
Raises:
  • IOError - if the file could not be opened.

from_transport(cls, t)
Class Method

source code 

Create an SFTP client channel from an open Transport.

Parameters:
Returns: SFTPClient
a new SFTPClient object, referring to an sftp session (channel) across the transport

get(self, remotepath, localpath, callback=None)

source code 

Copy a remote file (remotepath) from the SFTP server to the local host as localpath. Any exception raised by operations will be passed through. This method is primarily provided as a convenience.

Parameters:
  • remotepath (str) - the remote file to copy
  • localpath (str) - the destination path on the local host
  • callback (function(int, int)) - optional callback function that accepts the bytes transferred so far and the total bytes to be transferred (since 1.7.4)

Since: 1.4

get_channel(self)

source code 

Return the underlying Channel object for this SFTP session. This might be useful for doing things like setting a timeout on the channel.

Returns: Channel
the SSH channel

Since: 1.7.1

getcwd(self)

source code 

Return the "current working directory" for this SFTP session, as emulated by paramiko. If no directory has been set with chdir, this method will return None.

Returns: str
the current working directory on the server, or None

Since: 1.4

listdir(self, path='.')

source code 

Return a list containing the names of the entries in the given path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the folder. This method is meant to mirror os.listdir as closely as possible. For a list of full SFTPAttributes objects, see listdir_attr.

Parameters:
  • path (str) - path to list (defaults to '.')
Returns: list of str
list of filenames

listdir_attr(self, path='.')

source code 

Return a list containing SFTPAttributes objects corresponding to files in the given path. The list is in arbitrary order. It does not include the special entries '.' and '..' even if they are present in the folder.

The returned SFTPAttributes objects will each have an additional field: longname, which may contain a formatted string of the file's attributes, in unix format. The content of this string will probably depend on the SFTP server implementation.

Parameters:
  • path (str) - path to list (defaults to '.')
Returns: list of SFTPAttributes
list of attributes

Since: 1.2

lstat(self, path)

source code 

Retrieve information about a file on the remote system, without following symbolic links (shortcuts). This otherwise behaves exactly the same as stat.

Parameters:
  • path (str) - the filename to stat
Returns: SFTPAttributes
an object containing attributes about the given file

mkdir(self, path, mode=511)

source code 

Create a folder (directory) named path with numeric mode mode. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out.

Parameters:
  • path (str) - name of the folder to create
  • mode (int) - permissions (posix-style) for the newly-created folder

normalize(self, path)

source code 

Return the normalized path (on the server) of a given path. This can be used to quickly resolve symbolic links or determine what the server is considering to be the "current folder" (by passing '.' as path).

Parameters:
  • path (str) - path to be normalized
Returns: str
normalized form of the given path
Raises:
  • IOError - if the path can't be resolved on the server

open(self, filename, mode='r', bufsize=-1)

source code 

Open a file on the remote server. The arguments are the same as for python's built-in file (aka open). A file-like object is returned, which closely mimics the behavior of a normal python file object.

The mode indicates how the file is to be opened: 'r' for reading, 'w' for writing (truncating an existing file), 'a' for appending, 'r+' for reading/writing, 'w+' for reading/writing (truncating an existing file), 'a+' for reading/appending. The python 'b' flag is ignored, since SSH treats all files as binary. The 'U' flag is supported in a compatible way.

Since 1.5.2, an 'x' flag indicates that the operation should only succeed if the file was created and did not previously exist. This has no direct mapping to python's file flags, but is commonly known as the O_EXCL flag in posix.

The file will be buffered in standard python style by default, but can be altered with the bufsize parameter. 0 turns off buffering, 1 uses line buffering, and any number greater than 1 (>1) uses that specific buffer size.

Parameters:
  • filename (str) - name of the file to open
  • mode (str) - mode (python-style) to open in
  • bufsize (int) - desired buffering (-1 = default buffer size)
Returns: SFTPFile
a file object representing the open file
Raises:
  • IOError - if the file could not be opened.

put(self, localpath, remotepath, callback=None)

source code 

Copy a local file (localpath) to the SFTP server as remotepath. Any exception raised by operations will be passed through. This method is primarily provided as a convenience.

The SFTP operations use pipelining for speed.

Parameters:
  • localpath (str) - the local file to copy
  • remotepath (str) - the destination path on the SFTP server
  • callback (function(int, int)) - optional callback function that accepts the bytes transferred so far and the total bytes to be transferred (since 1.7.4)
Returns: SFTPAttributes
an object containing attributes about the given file (since 1.7.4)

Since: 1.4

readlink(self, path)

source code 

Return the target of a symbolic link (shortcut). You can use symlink to create these. The result may be either an absolute or relative pathname.

Parameters:
  • path (str) - path of the symbolic link file
Returns: str
target path

remove(self, path)

source code 

Remove the file at the given path. This only works on files; for removing folders (directories), use rmdir.

Parameters:
  • path (str) - path (absolute or relative) of the file to remove
Raises:
  • IOError - if the path refers to a folder (directory)

rename(self, oldpath, newpath)

source code 

Rename a file or folder from oldpath to newpath.

Parameters:
  • oldpath (str) - existing name of the file or folder
  • newpath (str) - new name for the file or folder
Raises:
  • IOError - if newpath is a folder, or something else goes wrong

rmdir(self, path)

source code 

Remove the folder named path.

Parameters:
  • path (str) - name of the folder to remove

stat(self, path)

source code 

Retrieve information about a file on the remote system. The return value is an object whose attributes correspond to the attributes of python's stat structure as returned by os.stat, except that it contains fewer fields. An SFTP server may return as much or as little info as it wants, so the results may vary from server to server.

Unlike a python stat object, the result may not be accessed as a tuple. This is mostly due to the author's slack factor.

The fields supported are: st_mode, st_size, st_uid, st_gid, st_atime, and st_mtime.

Parameters:
  • path (str) - the filename to stat
Returns: SFTPAttributes
an object containing attributes about the given file

symlink(self, source, dest)

source code 

Create a symbolic link (shortcut) of the source path at destination.

Parameters:
  • source (str) - path of the original file
  • dest (str) - path of the newly created symlink

truncate(self, path, size)

source code 

Change the size of the file specified by path. This usually extends or shrinks the size of the file, just like the truncate() method on python file objects.

Parameters:
  • path (str) - path of the file to modify
  • size (int or long) - the new size of the file

unlink(self, path)

source code 

Remove the file at the given path. This only works on files; for removing folders (directories), use rmdir.

Parameters:
  • path (str) - path (absolute or relative) of the file to remove
Raises:
  • IOError - if the path refers to a folder (directory)

utime(self, path, times)

source code 

Set the access and modified times of the file specified by path. If times is None, then the file's access and modified times are set to the current time. Otherwise, times must be a 2-tuple of numbers, of the form (atime, mtime), which is used to set the access and modified times, respectively. This bizarre API is mimicked from python for the sake of consistency -- I apologize.

Parameters:
  • path (str) - path of the file to modify
  • times (tuple(int)) - None or a tuple of (access time, modified time) in standard internet epoch time (seconds since 01 January 1970 GMT)