I need to get file and assign the contents to line array in python.
I am using psftp but I keep getting file error, I know that the file is in there:
Here is what I have:
import pysftp
myfile="daily_data_file_122300.csv"
sftp = pysftp.Connection('192.168.101.123', username='userid', password='userpassd')
sftp.cwd('userid')
sftp.get('myfile', preserve_mtime=True)
sftp.close()
I get this error:
IOError: [Errno 2] No such file
The file is there.
Related
This question already has an answer here:
ftplib.error_perm: 553 Could not create file. (Python 2.4.4)
(1 answer)
Closed 2 years ago.
I am just starting to learn Python. I'm trying to upload a file as follows:
import ftplib
myurl = 'ftp.example.com'
user = 'user'
password = 'password'
myfile = '/Users/mnewman/Desktop/requested.txt'
ftp = ftplib.FTP(myurl, user, password)
ftp.encoding = "utf-8"
ftp.cwd('/public_html/')
ftp.storbinary('STOR '+myfile, open(myfile, 'rb'))
But get the following error:
Traceback (most recent call last):
File "/Users/mnewman/.spyder-py3/temp.py", line 39, in <module>
ftp.storbinary('STOR '+myfile, open(myfile, 'rb'))
File "ftplib.pyc", line 487, in storbinary
File "ftplib.pyc", line 382, in transfercmd
File "ftplib.pyc", line 348, in ntransfercmd
File "ftplib.pyc", line 275, in sendcmd
File "ftplib.pyc", line 248, in getresp
error_perm: 553 Can't open that file: No such file or directory
What does "that file" refer to and what do I need to do to fix this?
Reading the traceback, the error is deep in the ftp stack processing a response from the server. FTP server messages aren't standardized, but from the text its clear that the FTP server is unable to write the file on the remote side. This can happen for a variety of reasons - perhaps there is a permissions problem (the identity of the FTP server process does not have rights to a target), the write is outside of a sandbox setup on the server, or even that its already open in another program.
But in your case, you are using the full source file name in the "STOR" command when it wants the target path. Depending on whether you want to write subdirectories on the server, calculating the target name can get complicated. If you just want the server's current working directory, you could
ftp.storbinary(f'STOR {os.path.split(myfile)[1]}', open(myfile, 'rb'))
"That file" refers to the file that you're trying to upload to the FTP. According to your code, it refers to the line: myfile = '/Users/mnewman/Desktop/requested.txt'. You get this error because Python can't find the file in the path. Check whether it exists in the correct path. If you want to test whether there is an error in the script, you can add a test file to the directory in which your Python script exists and then run the script with the path of that file.
Example Script for FTP Upload:
import ftplib
session = ftplib.FTP('ftp.example.com','user','password')
file = open('hello.txt','rb') # file to send
session.storbinary('STOR hello.txt', file) # send the file
file.close() # close file and FTP
session.quit()
I'm calling the Paramiko sftp_client.put(locapath,remotepath) method
This is throwing the [Errno 2] File not found error below.
01/07/2020 01:12:03 PM - ERROR - [Errno 2] File not found
Traceback (most recent call last):
File "file_transfer\TransferFiles.py", line 123, in main
File "paramiko\sftp_client.py", line 727, in put
File "paramiko\sftp_client.py", line 689, in putfo
File "paramiko\sftp_client.py", line 460, in stat
File "paramiko\sftp_client.py", line 780, in _request
File "paramiko\sftp_client.py", line 832, in _read_response
File "paramiko\sftp_client.py", line 861, in _convert_status
Having tried many of the other recommend fixes I found that the error is due to the server having an automatic trigger to move the file immediately to another location upon the file being uploaded.
I've not seen another post relating to this issue and wanted to know if anyone else has fixed this as the SFTP server is owned by a third party and not wanting to change trigger attributes.
The file actually uploads correctly, so I could catch the Exception and ignore the error. But I'd prefer to handle it, if possible.
Paramiko by default verifies a size of the uploaded file after the upload.
If the file is moved away immediately after upload, the check fails.
To avoid the check, set confirm parameter of SFTPClient.put to False.
sftp_client.put(localpath, remotepath, confirm=False)
I believe the check is redundant anyway, see
How to perform checksums during a SFTP file transfer for data integrity?
For a similar question about pysftp (what is a wrapper around Paramiko), see:
Python pysftp.put raises "No such file" exception although file is uploaded
Also had this issue of the file automatically getting moved before paramiko could do an os.stat on the uploaded file and compare the local and uploaded file sizes.
#Martin_Prikryl solution works works fine for removing the error by passing in confirm=False when using sftp.put or sftp.putfo
If you want this check to still run like you mention in the post to see if the file has been uploaded fully you can run something along these lines. For this to work you will need to know the moved file location and have the ability to read the file.
import os
sftp.putfo(source_file_object, destination_file, confirm=False)
upload_size = sftp.stat(moved_path).st_size
local_size = os.stat(source_file_object).st_size
if upload_size != local_size:
raise IOError(
"size mismatch in put! {} != {}".format(upload_size, local_size)
)
Both checks use os.stat
I'm trying to copy files from SFTP server .
I can connect using python pysftp .
I can run:
data = srv.listdir()
for i in data:
print I
And I get the Directory list. But when I try
sftp.put (localpath,"file_name.txt")
I get
>"IOError: [Errno 13] Permission denied: 'C:\\....."
I have permission to that folder, because I can run MKDIR and it creates a directory in that file path. I have tried many many different ways but no luck so far, any help is truly appreciated.
import pysftp
import os
def sftpExample():
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
with pysftp.Connection('HOST', username='username', password='Password', cnopts=cnopts) as sftp :
print 'connected '
localpath="C:\\new project\\new"
remotepath="/folder1"
sftp.put(localpath,"infso.txt")
sftp.put(localpath,remotepath)
sftp.getfo (remotepath, localpath )
srv.get_r(localpath, remotepath)
srv.close()
sftpExample()
I get this error code:
Traceback (most recent call last):
File "db_backup.py", line 42, in <module>
sftpExample()
File "db_backup.py", line 17, in sftpExample
sftp.put(localpath,"GT-Dallas SFTP infso.txt")
File "c:\Python27\lib\site-packages\pysftp\__init_.py", line 364, in put
confirm=confirm)
File "c:\Python27\lib\site-packages\paramiko\sftp_client.py", line 720, in put
with open(localpath, 'rb') as fl:
IOError: [Errno 13] Permission denied: "C:\\new project\\new"
I've tried all different ways to copy the file as you see however I've had no luck so far.
The issue is that you're trying to save a file as a directory which, at least in my experience, is what throws the Permission Denied error in pysftp.
Change this line of code:
localpath="C:\\new project\\new"
To this:
localpath="C:\\new project\\new\\infso.txt"
NOTE: infso.txt can be anything you want to name the local file being downloaded. I've used the same name here as the remote file's name from your example for symmetry and simplicity.
There are a few things that might be causing your issue, but the one that stands out to me comes from your error message:
IOError: [Errno 13] Permission denied: "C:\\new project\\new"
It might be that you need to escape the space ("\ ") or put it in a raw string r"C:\My Path With Spaces"
But in any case, I would avoid using spaces in your filenames, and you should rename your project folder to something like new_project or newproject.
Another thing that would make your life easier is if you compressed your directory into a single archive file (.tgz or .zip or something) and transfer that file.
I am trying to load remote server certificate and save it in my local disk. This is the python script I'm using:
from M2Crypto.X509 import FORMAT_PEM
import StringIO
import traceback
from M2Crypto.Err import SSLError
import ssl
import socket
import pprint
import M2Crypto
from M2Crypto import X509, RSA
from datetime import datetime
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
port = 443
host='216.58.212.67' #google
#creating ssl socket
ssock = context.wrap_socket(socket.socket(socket.AF_INET), server_hostname=host)
#ssl connection
try:
ssock.connect((host, port))
except socket.error: #if tls connection is not possible
print "Faile connection with: " + host
#get the certificate
cert = ssock.getpeercert(True)
x509 = M2Crypto.X509.load_cert_der_string(cert)
x509_file= M2Crypto.X509.load_cert('C:/Users/xxx/Documents/temp',format=FORMAT_PEM)
When I run it, I get this error:
Traceback (most recent call last):
File "C:/Users/ealas/PycharmProjects/tlsScan/test.py", line 36, in <module>
x509_file= M2Crypto.X509.load_cert('C:/Users/xxx/Documents/temp',format=FORMAT_PEM)
File "C:\Python27\lib\site-packages\M2Crypto\X509.py", line 609, in load_cert
bio = BIO.openfile(file)
File "C:\Python27\lib\site-packages\M2Crypto\BIO.py", line 186, in openfile
return File(open(filename, mode))
IOError: [Errno 13] Permission denied: 'C:/Users/xxx/Documents/temp'
What is wrong in my code please?
You're specifying a folder when you should be specifying a file. According to the documentation for the M2Crypto.X509.load_cert function, you should be specifying the path to a file, not a folder:
Load certificate from file.
#type file: string
#param file: Name of file containing certificate in either DER or PEM format.
If you try and load data from or write data to a folder instead of a file, you will get a "Permission denied" error, at least on Windows. To test this, I created a folder called temp, and tried to read data from it and write data to it, and I got the exact same error as in your question:
Traceback (most recent call last):
File "test.py", line 4, in <module>
with open(r'C:\Users\Random\Documents\temp', 'w') as f:
IOError: [Errno 13] Permission denied: 'C:\\Users\\Random\\Documents\\temp'
Traceback (most recent call last):
File "test.py", line 4, in <module>
with open(r'C:\Users\Random\Documents\temp', 'r') as f:
IOError: [Errno 13] Permission denied: 'C:\\Users\\Random\\Documents\\temp'
In the future, you should look at the documentation for the functions you're using to ensure that you're passing not only the right type of variable to it, but also that the data itself is what the function expects.
Also, in your question you said you're trying to write to a file, but you're using a function which reads from a file. I would suggest going through and making sure you're doing what you think you're doing. Again, reading the documentation for the library you're using will be helpful.
I have the following log file called log.txt, with all the file names to be considered from a folder:
log.txt
C:\data\01.log
C:\data\02.log
C:\data\03.log
C:\data\04.log
My task is to read these files one after another from log.txt using a for loop.
with open("C:\data\log.txt",'r') as f:
logs=f.read()
print logs
for line in logs:
line = myfile.readline().replace('\n', '')
with open(line, 'r') as myfile:
lines = [line.rstrip('\n') for line in myfile.readlines()]
I am getting this error:
IOError: [Errno 2] No such file or directory:
What is the error you are getting?
Is it "IOError: [Errno 2] No such file or directory:"?
This error means that the directory C:\data\ does not exist. Are you sure this folder exists? Also if it does exist, is the logs.txt file in that directory?
I personally do not have a C:\data directory, so unless you created it, you have the address of the wrong directory.