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.
Related
I'm trying to download a file but when it's trying to write to the current directory it gives a permission error
Traceback (most recent call last):
File "C:\Users\HP User\Desktop\WWE Tool\MasterDownload.py", line 22, in <module>
with open(x, 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\HP User\\Desktop\\WWE Tool'
Code:
MasterDownload = requests.get(url=Master, headers=Heads)
fpath = os.getcwd()
with open(fpath, 'wb') as f:
f.write(MasterDownload.content)
I checked the current path and eveything looks fine, I just can't get around as to why it's not writing as I am an admin
You're actually trying to write to a directory (the process' current working directory - as obtained from os.getcwd()), not to a file. Try selecting an actual file in that directory to write to instead of the directory itself, and the issue might go away.
I work for a photography studio that shoots and retouches thousands of images per day. The studio has been moving files manually for many years and we experience a lot of user error placing the files/folders in the wrong place. To help eliminate this I created a Python script (run via Apple Automator droplet application) which: 1. moves the files from the photographer's computer to the file server, 2. creates new directories if they do not already exist and 3. does name versioning changes on the file names if files with the same name already exist.
Here is the script:
import sys
import os
import subprocess
destinationPath = '/Volumes/extensis/product'
def updateName(name, incrementCounter):
bsc = name[:19]
seq = name[19:20]
ext = name[20:]
try:
seq_new = int(seq)
if incrementCounter:
seq_new = seq_new + 1
except ValueError:
seq_new = 1
name = bsc + str(seq_new).zfill(1) + ext
return name
def makeSureCounterIsNumeric(name):
return updateName(name, False)
def incrementCounter(name):
return updateName(name, True)
for f in sys.argv[1:]:
name = os.path.basename(f)
newdir = os.path.join(destinationPath, name[:1], name[:5], name[:10], name[:15])
print newdir
if not os.path.exists(newdir):
os.makedirs(newdir)
name = makeSureCounterIsNumeric(name)
while os.path.isfile(os.path.join(newdir,name)):
name = incrementCounter(name)
subprocess.call(['cp', f, os.path.join(newdir,name)])
This is working great on MOST computers without issue. Some computers can't run the application at all and get a python makedirs/permission denied error. Some computers have been able to run the application for a period of time and then all of a sudden have this same error. Sometimes this error clears if we restart the computer, but not always. Traceback:
Traceback (most recent call last):
File "<string>", line 31 in <module>
File "/System/Library/Frameworks/Python.framework/Version/2.7/lib/python2.7/os.py", Line 150, in makedirs makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Version/2.7/lib/python2.7/os.py", Line 150, in makedirs makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Version/2.7/lib/python2.7/os.py", Line 150, in makedirs makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Version/2.7/lib/python2.7/os.py", Line 150, in makedirs makedirs(head, mode)
File "/System/Library/Frameworks/Python.framework/Version/2.7/lib/python2.7/os.py", Line 157, in mkdirs mkdirs(name, mode)
OSError: [Errno 13] Permission denied: '/Volumes/extensis/product'
We have confirmed that all local machines are running the same version of the OS/python, all have admin permissions for their individual machines, all are connecting to the same file server address, and everyone has the correct permissions to access/modify the server location. They can even manually make the directories or move files to this location without issue.
I also found some people on StackOverflow mentioning that the "/" used in the volume path can cause these issues because it refers to root folders, but I have tried all variants of the path. With "/", without "/" and the above is the only variation that I have been able to run successfully. The other variants either throw the permissions error or appear to run successfully but no files have been moved.
We have done a bunch of research without any answers. We've talked with our local IT department and software devs and nobody knows anything different to look at. The only thing they have suggested is that perhaps it is just a Mac-to-Windows issue and might not be anything we can fix. That doesn't help.
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.
I am working on Ubuntu and writing a code in python. I want to add a line in a file which is placed in root directory:
ins = open( "/usr/local/etc/conf.d/test.txt", "r" )
array = []
for line in ins:
array.append( line )
array.append('add this new line')
f = open("/usr/local/etc/gnuradio/test.txt",'w')
for line in array:
f.write(line)
I am getting this error:
Traceback (most recent call last):
File "overwrite.py", line 6, in <module>
f = open("/usr/local/etc/gnuradio/test.txt",'w')
IOError: [Errno 13] Permission denied: '/usr/local/etc/gnuradio/test.txt'
I know we do not have permission to change anything in root directory without using sudo. But is there anyway I can update this file from within my python module?
You already answered your own question: You do not have the permission to do so.
No matter if you use sh, bash, python, C, erlang or a rubber-hose attack.
Either run your script with a user owning the necessary permissions or grant yourself access to the file.
shutil.copy() is raising a permissions error:
Traceback (most recent call last):
File "copy-test.py", line 3, in <module>
shutil.copy('src/images/ajax-loader-000000-e3e3e3.gif', 'bin/styles/blacktie/images')
File "/usr/lib/python2.7/shutil.py", line 118, in copy
copymode(src, dst)
File "/usr/lib/python2.7/shutil.py", line 91, in copymode
os.chmod(dst, mode)
OSError: [Errno 1] Operation not permitted: 'bin/styles/blacktie/images/ajax-loader-000000-e3e3e3.gif'
copy-test.py:
import shutil
shutil.copy('src/images/ajax-loader-000000-e3e3e3.gif', 'bin/styles/blacktie/images')
I am running copy-test.py from the command line:
python copy-test.py
But running cp from the command line on the same file to the same destination doesn't cause an error. Why?
The operation that is failing is chmod, not the copy itself:
File "/usr/lib/python2.7/shutil.py", line 91, in copymode
os.chmod(dst, mode)
OSError: [Errno 1] Operation not permitted: 'bin/styles/blacktie/images/ajax-loader-000000-e3e3e3.gif'
This indicates that the file already exists and is owned by another user.
shutil.copy is specified to copy permission bits. If you only want the file contents to be copied, use shutil.copyfile(src, dst), or shutil.copyfile(src, os.path.join(dst, os.path.basename(src))) if dst is a directory.
A function that works with dst either a file or a directory and does not copy permission bits:
def copy(src, dst):
if os.path.isdir(dst):
dst = os.path.join(dst, os.path.basename(src))
shutil.copyfile(src, dst)
This form worked for me:
shutil.copy('/src_path/filename','/dest_path/filename')
I had this exact same issue... using shutil.copyfile() was a good workaround.... however, I completely FIXED the issue by simply rebooting Windows 10 OS.
This work for me:
I don't use shutil I use shell command for copy, and run shell command in python.
Code:
import os
my_source= '/src_path/filename'
my_dest= '/dest_path/filename'
os.system('sudo cp ' +my_source + my_dest ) # 'sudo cp /src_path/filename /dest_path/filename '
and that work for me.
This is kind of a guess, but the first thing that pops out at me:
'bin/styles/blacktie/images'
You have no trailing slash. While I'm not sure of the implementation of shutil.copy(), I can tell you that cp will act differently depending on what OS you're running it on. Most likely, on your system, cp is being smart and noticing that images is a directory, and copying the file into it.
However, without the trailing slash, shutil.copy() may be interpreting it as a file, not checking, and raising the exception when it's unable to create a file named images.
In short, try this:
'bin/styles/blacktie/images/'
Arguments must be:
shutil.copy('src/images/ajax-loader-000000-e3e3e3.gif', 'bin/styles/blacktie/images.ajax-loader-000000-e3e3e3.gif')