Logo Search packages:      
Sourcecode: paramiko version File versions  Download package

def paramiko::sftp_client::SFTPClient::file (   self,
  filename,
  mode = 'r',
  bufsize = -1 
)

Open a file on the remote server.  The arguments are the same as for
python's built-in C{file} (aka C{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: C{'r'} for reading,
C{'w'} for writing (truncating an existing file), C{'a'} for appending,
C{'r+'} for reading/writing, C{'w+'} for reading/writing (truncating an
existing file), C{'a+'} for reading/appending.  The python C{'b'} flag
is ignored, since SSH treats all files as binary.  The C{'U'} flag is
supported in a compatible way.

Since 1.5.2, an C{'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
C{O_EXCL} flag in posix.

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

@param filename: name of the file to open.
@type filename: string
@param mode: mode (python-style) to open in.
@type mode: string
@param bufsize: desired buffering (-1 = default buffer size)
@type bufsize: int
@return: a file object representing the open file.
@rtype: SFTPFile

@raise IOError: if the file could not be opened.

Definition at line 156 of file sftp_client.py.

00156                                                   :
        """
        Open a file on the remote server.  The arguments are the same as for
        python's built-in C{file} (aka C{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: C{'r'} for reading,
        C{'w'} for writing (truncating an existing file), C{'a'} for appending,
        C{'r+'} for reading/writing, C{'w+'} for reading/writing (truncating an
        existing file), C{'a+'} for reading/appending.  The python C{'b'} flag
        is ignored, since SSH treats all files as binary.  The C{'U'} flag is
        supported in a compatible way.
        
        Since 1.5.2, an C{'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
        C{O_EXCL} flag in posix.

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

        @param filename: name of the file to open.
        @type filename: string
        @param mode: mode (python-style) to open in.
        @type mode: string
        @param bufsize: desired buffering (-1 = default buffer size)
        @type bufsize: int
        @return: a file object representing the open file.
        @rtype: SFTPFile

        @raise IOError: if the file could not be opened.
        """
        filename = self._adjust_cwd(filename)
        imode = 0
        if ('r' in mode) or ('+' in mode):
            imode |= SFTP_FLAG_READ
        if ('w' in mode) or ('+' in mode) or ('a' in mode):
            imode |= SFTP_FLAG_WRITE
        if ('w' in mode):
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_TRUNC
        if ('a' in mode):
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_APPEND
        if ('x' in mode):
            imode |= SFTP_FLAG_CREATE | SFTP_FLAG_EXCL
        attrblock = SFTPAttributes()
        t, msg = self._request(CMD_OPEN, filename, imode, attrblock)
        if t != CMD_HANDLE:
            raise SFTPError('Expected handle')
        handle = msg.get_string()
        return SFTPFile(self, handle, mode, bufsize)

    # python has migrated toward file() instead of open().
    # and really, that's more easily identifiable.
    open = file


Generated by  Doxygen 1.6.0   Back to index