Results 1 to 7 of 7
Thread: Python SFTP help
-
2021-02-11, 02:24 PM (ISO 8601)
- Join Date
- Jan 2009
Python SFTP help
Note: a few months ago I asked about something similar, but it was for FTP.
I've now got to programmatically upload files to an SFTP (really SFTP this time) site, using Python.
I found useful advice at
https://stackoverflow.com/questions/...rm-independent
but I'm having some issues and having trouble following.
Passwords, username, and host are changed in the below.
Code:host='sftp.cloud.something.com' user='myusername' pw='mypassword filepath="\\\s1prod\\office\\myfolder" filename=filepath+'\manifest.csv' import paramiko port = 22 transport = paramiko.Transport((host, port)) transport.connect(username = user, password = pw) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(filename,'manifest.csv') sftp.close() transport.close()
sftp.put(filename,'manifest.csv')
so I think I am able to log into the SFTP site correctly, but I don't really understand PUT statements. (I have logged in via WinSCP successfully--but I want to to do this programmatically since I have to upload files to several different SFTP accounts.)
Here's the error log, taking out my name from the C: path.
Code:Traceback (most recent call last): File "<ipython-input-13-7f70cba9d8eb>", line 10, in <module> sftp.put(filename,'manifest.csv') File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 759, in put return self.putfo(fl, remotepath, file_size, callback, confirm) File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 714, in putfo with self.file(remotepath, "wb") as fr: File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 372, in open t, msg = self._request(CMD_OPEN, filename, imode, attrblock) File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 813, in _request return self._read_response(num) File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 865, in _read_response self._convert_status(msg) File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 896, in _convert_status raise IOError(errno.EACCES, text) PermissionError: [Errno 13] Permission denied
Spoiler: one issue I know isn't the case
I was worried maybe my syntax is wrong for the program to find the CSV that I want to put on the SFTP site.
But this code worked fine:
Code:#testing that Python can find the file--it can! import pandas mydata=pandas.read_csv(filename) print(mydata)
Last edited by JeenLeen; 2021-02-11 at 02:50 PM.
-
2021-02-11, 02:50 PM (ISO 8601)
- Join Date
- Jun 2007
Re: Python SFTP help
It's a put error, yes.
PermissionError: [Errno 13] Permission denied
As for something to try:
filepath='\\\\s1prod\\office\\myfolder'
filename=filepath+ '\\manifest.csv'May you get EXACTLY what you wish for.
-
2021-02-11, 02:52 PM (ISO 8601)
- Join Date
- Jul 2004
- Location
- Freiburg, germany
- Gender
Re: Python SFTP help
a) Nitpick: filename=filepath+'\manifest.csv' should be filename=filepath+'\\manifest.csv' - shouldn't change your error, though
b) you're correct, you're getting the error on put() - specifically, the other end is telling you that wou don't have write permissions on the target path. Check the permissions on the SFTP end for your user. Try it with Filezilla to make sure it's a code problem and not a receiver problem, maybe?
-
2021-02-11, 03:18 PM (ISO 8601)
- Join Date
- Jan 2009
Re: Python SFTP help
Upon testing in WinSCP, I learnt that I don't have access to the root directory but do have one to an "uploads" folder. I wish the person had told me that.
I fixed a couple things, and it's no longer giving me an error, but neither is a file showing up in the SFTP site. Here's the revised snippet of code
Code:filename=filepath+'\\manifest.csv' #testing that Python can find the file--it can! #import pandas #mydata=pandas.read_csv(filename) #print(mydata) import paramiko port = 22 transport = paramiko.Transport((host, port)) transport.connect(username = user, password = pw) sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(filename,'./uploads/manifest.csv') sftp.close() transport.close()
sftp.put(filename,'./uploads/manifest.csv')
EDIT
I've been trying various forward- and backslashes in the name.
If I try
Code:sftp.put(filename,'.\\uploads\\manifest.csv')
Code:File "C:\Users\AppData\Local\Continuum\anaconda3\lib\site-packages\paramiko\sftp_client.py", line 896, in _convert_status raise IOError(errno.EACCES, text) PermissionError: [Errno 13] Permission denied
b) you're correct, you're getting the error on put() - specifically, the other end is telling you that wou don't have write permissions on the target path. Check the permissions on the SFTP end for your user. Try it with Filezilla to make sure it's a code problem and not a receiver problem, maybe?
Is there a way to check the SFTP permissions beyond just asking them "Hey, is there something blocking Python code from sending you files?"Last edited by JeenLeen; 2021-02-11 at 03:27 PM.
-
2021-02-11, 03:27 PM (ISO 8601)
- Join Date
- Jun 2007
Re: Python SFTP help
Try getting with the other side.
Since it's an CSV file, I'm going to assume you're uploading to some sort of service that processes a standardized CSV and stores information for reporting and analysis.
Some of those services won't let you list the directory, or, upon upload, have it almost immediately move to a 'processing' folder that's not visible to you.
And try sftp.put(filename,'./uploads/manifest.csv')Last edited by sihnfahl; 2021-02-11 at 03:30 PM.
May you get EXACTLY what you wish for.
-
2021-02-11, 03:35 PM (ISO 8601)
- Join Date
- Jan 2009
Re: Python SFTP help
I uploaded a file via WinSCP, exited WinSCP, and re-entered the SFTP site. The file is gone. So maybe that is what happened. I e-mailed my contact to ask him if the file is there or not.
If it is there, I think my program is working now. I just can't confirm it on my end, which is frustrating. But, hey, at least it seems to be working.
Many thanks, and hopefully my next post will be saying "yep, it's good" and not a follow-up question!
-
2021-02-12, 01:53 AM (ISO 8601)
- Join Date
- Jul 2004
- Location
- Freiburg, germany
- Gender
Re: Python SFTP help
For all things files and paths you should really give pathlib a look, especially under windows or when writing something that's supposed to be portable.
Unfortunately, I only have my work computer, and I can't download new software on it. So I can't check via Filezilla. But it is working via WinSCP login.
Is there a way to check the SFTP permissions beyond just asking them "Hey, is there something blocking Python code from sending you files?"
Code:username ="youruser" password = "yourpass" host = "sftphost" port = int("sftpport") ssh = paramiko.SSHClient() ssh.connect(host, port, username, password, look_for_keys=False) stdin, stdout, stderr = ssh.exec_command("ls -la") print(stdout.readlines())