I am wondering how to read and write files to an SFTP server in Python. I tried searching for other Stackoverflow posts, but they all suggested using outdated modules like pysftp, or other modules that rely on pysftp. Does anyone know any SFTP Modules for Python?
82805
I'm working on a simple tool that transfers files to a hard-coded location with the password also hard-coded.  I'm a python novice, but thanks to ftplib, it was easy:  import ftplib  info= ('someuser', 'password')    #hard-coded  def putfile(file, site, dir, user=(), verbose=True):     """     upload a file by ftp to a site/directory     login hard-coded, binary transfer     """     if verbose: print 'Uploading', file     local = open(file, 'rb')         remote = ftplib.FTP(site)        remote.login(*user)              remote.cwd(dir)     remote.storbinary('STOR ' + file, local, 1024)     remote.quit()     local.close()     if verbose: print 'Upload done.'  if __name__ == '__main__':     site = 'somewhere.com'            #hard-coded     dir = './uploads/'                #hard-coded     import sys, getpass     putfile(sys.argv[1], site, dir, user=info)   The problem is that I can't find any library that supports sFTP.  What's the normal way to do something like this securely?    Edit:  Thanks to the answers here, I've gotten it working with Paramiko and this was the syntax.  import paramiko  host = "THEHOST.com"                    #hard-coded port = 22 transport = paramiko.Transport((host, port))  password = "THEPASSWORD"                #hard-coded username = "THEUSERNAME"                #hard-coded transport.connect(username = username, password = password)  sftp = paramiko.SFTPClient.from_transport(transport)  import sys path = './THETARGETDIRECTORY/' + sys.argv[1]    #hard-coded localpath = sys.argv[1] sftp.put(localpath, path)  sftp.close() transport.close() print 'Upload done.'   Thanks again!
12371
This question originally asked which is the best method for uploading files via SFTP or FTPS in SSIS.  It now just lists the pros and cons of each solution.  I personally use CozyRoc's SFTP library these days, but I've used each of the below solutions at one point or another.  SSIS COMPONENT LIBRARY  Method: Install a SSIS component library from , , , , or some other vendor on each development and production server and use the SFTP task to upload the files.  Pros: Easy to use.  It looks, smells, and feels like a normal SSIS task.  SSIS also recognizes the password as sensitive information and allows you all the normal options for protecting the sensitive information instead of just storing it in clear text in a non-secure manner.  Works well with other SSIS tasks such as ForEach Loop Containers.  Errors out when uploads and downloads fail.  Works well when you don't know the names of the files on the remote FTP site to download or when you won't know the name of the file to upload until run-time.  Cons: With the exception of the Codeplex solution, this costs money to license in a production environment.  Requires installing the libraries on each development and production machine.  If it is the Codeplex solution, then you are using software that isn't supported by any specific vendor.  This also makes you dependent upon the vendor to update their libraries between each version.  For instance, before 2008 RTM'd, I was developing a new server on a CTP version of 2008 and the CozyRoc 2005 library was incompatible with it.  Eventually they released a 2008 compatible version, but I had to temporarily use the command line solution to work around this issue.    COMMAND LINE SFTP PROGRAM  Method: Install a free command-line SFTP application such as Putty and WinSCP and execute it either by running a batch file or operating system process task.  Instructions for doing this via WinSCP are listed .  Pros: Free, free, and free.  You can be sure it is secure if you are using Putty since numerous GUI FTP clients appear to use Putty under the covers.  You DEFINATELY know you are using SSH2 and not SSH.  Cons: The two command-line utilities I tried (Putty and Cygwin) required storing the SFTP password in a non-secure location.  I haven't found a good way to capture failures or errors when uploading files.  The process doesn't look and smell like SSIS.  Most of the code is encapsulated in text files instead of SSIS itself.  Difficult to use if you don't know the exact name of the file you are uploading or downloading.  A 3RD PARTY C# or VB.NET LIBRARY  Method: Install a SFTP or FTPS library and use a Script Task that references the library to upload the files.  (I've never tried this, so I'm going to guess at the pros and cons)  Pros: Probably easy to capture errors.  Should work well with variables, so it would probably be easy to use even when you don't know the exact name of the file you are uploading or downloading.  Cons: It's a script task combined with .NET libraries.  If you are using SSIS, then you probably are more comfortable with SSIS tasks then .NET code.  Script tasks are also difficult to troubleshoot since they don't have the same debugging tools and features as regular .NET projects.  Creates a dependency on 3rd party code that may not work between different versions of SQL Server.  To be fair, it is probably MORE likely to work between different versions of SQL Server than a 3rd party SSIS task library.  Another huge con -- I haven't found a free C# or VB.NET library that does this as of yet.  So if anyone knows of one, then please let me know!
110447
In Python you can use  for a file-like buffer for character data.  basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python have a file object that is intended for binary data and is memory only, equivalent to Java's ?  The use-case I have is I want to create a ZIP file in memory, and  requires a file-like object.
46689
I'm reading source code of the linux tool . They use the read() function there. Is there a difference to the standard C fread() function? (I'm not counting the arguments as a difference.)
113801
I am trying to run a script within Blender that utilizes multiprocessing module. According to documentation it is possible to use it ( see note at the end of  ).     Pythons threads only allow co-currency and won’t speed up your scripts on multi-processor systems, the subprocess and multiprocess modules can be used with Blender and make use of multiple CPU’s too..   So I tried running below script within Blender:  from multiprocessing import Pool  def test( v ):     print(v)     return v  p = Pool(2)  print( p.map(test, range(5) ) )   This however spawns as many new Blender instances (with separate windows) as there are workers in p (in my case 2). Also it spews in System Console :  Can't read file: "C:\Program Files\Blender Foundation\Blender\-c", Unable to open the file. Can't read file: "C:\Program Files\Blender Foundation\Blender\from multiprocessing.forking import main; main()", Unable to open the file. Can't read file: "C:\Program Files\Blender Foundation\Blender\--multiprocessing-fork", Unable to open the file. Can't read file: "C:\Program Files\Blender Foundation\Blender\2888", Unable to open the file.   I also thought about using os.fork() to do the same thing, but sadly Windows does not support that.  I've been looking all over Internet, but so far I was not able to find even one example proving that documentation is correct.  The only thing I found was , but it is locked and with no responses.    Update: This is a windows spesific limitation in the multiprocessing module. ~ideasman42.
9285
When trying to write the stdout from a Python script to a text file (python script.py &gt; log), the text file is created when the command is started, but the actual content isn't written until the Python script finishes. For example:  script.py:  import time for i in range(10):     print('bla')     time.sleep(5)   prints to stdout every 5 seconds when called with python script.py, but when I call python script.py &gt; log, the size of the log file stays zero until the script finishes. Is it possible to directly write to the log file, such that you can follow the progress of the script (e.g. using tail)?   EDIT It turns out that python -u script.py does the trick, I didn't know about the buffering of stdout.
123393
Is there a way to upload a file to a FTP server when behind an HTTP proxy ?  It seems that uploading a file is not supported behind an HTTP Proxy using .Net Webclient. ().  If there is no workaround ? If not, do you know a good and free FTP library I can use ?  Edit: Unfortunately, I don't have any FTP proxy to connect to.
83516
Is there a way to send a file using POST from a Python script?
93776
I'm trying to write out a  Byte[] array representing a complete file to a file.  The original file from the client is sent via TCP and then received by a server. The received stream is read to a byte array and then sent to be processed by this class.   This is mainly to ensure that the receiving TCPClient is ready for the next stream and separate the receiving end from the processing end.   The FileStream class does not take a byte array as an argument or another Stream object ( which does allow you to write bytes to it).  I'm aiming to get the processing done by a different thread from the original ( the one with the TCPClient).   I don't know how to implement this, what should I try?
64135
I'm using Python, and would like to insert a string into a text file without deleting or copying the file. How can I do that?
40839
I'm starting a very little hosting company for a few friends and little clients, nothing big.  I want to give my "clients" the right to manage their files on the server. I hate FTP as it is not secure and it's in my opinion obsolete.  So I'd like to allow my users to connect through SFTP but not allow them to connect through SSH. (I know, I know, SFTP is using SSH). But I was just wondering, is it possible?  So I wouldn't have to install a FTP service on the server and everything would be awesome!
92898
I have a Linux machine, and I need to sftp to a Windows SFTP server. So for first step, I create my own id_rsa file and the id_rsa.pub in my Linux machine.  Then I copy the text in the id_rsa.pub into the id_rsa.pub in the SFTP server.  And the sftp connection work correctly.  However, I would like to ask about the command to copy the public key from client to server. I have search in google and I get a command which is:  ssh-copy-id -i id_rsa.pub ftp_user*@10.7.8.32   But I hit the following error:     'exec' is not recognized as an internal or external command, operable program or batch file.       The system cannot find the path specified.     I believe there is some command exits for this right? Instead of I copy the public key manually to the SFTP server.  The SFTP version is SFTP protocol version 3.
8955
I'm using this code to get standard output from an external program:  &gt;&gt;&gt; from subprocess import * &gt;&gt;&gt; command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]   The communicate() method returns an array of bytes:  &gt;&gt;&gt; command_stdout b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'   However, I'd like to work with the output as a normal Python string. So that I could print it like this:  &gt;&gt;&gt; print(command_stdout) -rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1 -rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2   I thought that's what the  method is for, but when I tried it, I got the same byte array again:  &gt;&gt;&gt; binascii.b2a_qp(command_stdout) b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar  3 07:03 file2\n'   How do I convert the bytes value back to string? I mean, using the "batteries" instead of doing it manually. And I'd like it to be OK with Python&nbsp;3.
98531
I have just installed Ubuntu Server 10.04 with a LAMP setup.  I want to host a website there but I'm not sure which is the best way to get/edit my files on the server.  Googling provides many options but I'm not sure which is best?  Unless there's a better option, I'd like to be create the pages on another PC (Windows or Linux) and use SFTP to sync the changes to the server - but do I do this to a symlinked folder in ~/ or by changing rights on the /var/www/ folder?
7655
Is there local file manipulation that's been done with JavaScript? I'm looking for a solution that can be accomplished with no install footprint like requiring .  Specifically, I'd like to read the contents from a file and write those contents to another file. At this point I'm not worried about gaining permissions and am just assuming I already have full permissions to these files.
78831
On Windows I'm using FileZilla to upload/download files on my WordPress website, via sftp, while using ssh key.   Now Linux is my primary operating system. For the last month FileZilla client was working fine on Ubuntu 16.04, but now it crashes whenever I start it. I search on internet and found that it is happening with a lot of users. Unfortunately the FileZilla support just have one answer if we ask them:     Ubuntu 16.04 is old OS, use 17.10 or Debian latest version.   I don't want to use 17.10. So I am looking for FileZilla client alternative for Linux, which I can use as sftp client with ssh key.   I found gFTP (which is an old program and I'm not sure how much secure is it?), I also found that I can use Ubuntu file manager to connect to my server. Is that a good idea to connect to server while using Nautilus or Caja?   Actually I don't want to put my username / password in dialogue box which appear when I click File > Connect to server, instead I want to use my .ppk key. How I can add that in Nautilus or Caja?
110498
That's it. If you want to document a function or a class, you put a string just after the definition. For instance:  def foo():     """This function does nothing."""     pass   But what about a module? How can I document what a file.py does?