Permission denied when trying to copy files - python

Using the script:
file = os.path.join(subfolder_name, list_of_files[i])
for dest_folder_finetune, dest_folder_relab in zip(finetune_datasets, relab_datasets):
copy(file, dest_folder_finetune)
copy(file, dest_folder_relab)
every 20 iterations, I am having a permission denied problem. The stack, looks something like:
Traceback (most recent call last):
File "/home/revan/boosting_classifier_with_games/dataset_creator.py", line 72, in <module>
main()
File "/home/revan/boosting_classifier_with_games/dataset_creator.py", line 24, in main
create_test_and_relab(list_of_subfolders)
File "/home/revan/boosting_classifier_with_games/dataset_creator.py", line 66, in create_test_and_relab
copy(file, dest_folder_finetune)
File "/home/revan/anaconda2/envs/pytorch/lib/python2.7/shutil.py", line 119, in copy
copyfile(src, dst)
File "/home/revan/anaconda2/envs/pytorch/lib/python2.7/shutil.py", line 83, in copyfile
with open(dst, 'wb') as fdst:
IOError: [Errno 13] Permission denied: '/sun_btbivuchmkkzetpo.jpg'
The 'funny' thing is that I definitely have all permissions for that file. Furthermore, I tried to copy it manually, and I can do it without problem. If I randomize the process, the same file gets copied, but then some other file (after 20 iterations) cannot be copied.
PS: Changing copy to copy2, gives the exact same problem.
Has anyone experienced anything similar in the past? Could it be a Python or a Linux problem?

One of your datasets has a bad folder name, which is setting the destination to the root of the file system.
To just get it working, you can skip when it's trying to copy to the root:
import os
file = os.path.join(subfolder_name, list_of_files[i])
for dest_folder_finetune, dest_folder_relab in zip(finetune_datasets, relab_datasets):
if os.path.abspath(dest_folder_finetune) != "/":
copy(file, dest_folder_finetune)
else:
print("Warning, path {} for dest_folder_finetune writes to the root of the filesystem".format(dest_folder_finetune))
if os.path.abspath(dest_folder_relab) != "/":
copy(file, dest_folder_relab)
else:
print("Warning, path {} for dest_folder_relab writes to the root of the filesystem".format(dest_folder_relab ))
However, if this is more than a live once script, I suggest scrubbing and verifying the dataset beforehand instead.

try with "sudo python filename.py"

Related

python giving error in extracting a file from the file

Python code:
import tarfile,os
import sys
def extract_system_report (tar_file_path):
extract_path = ""
tar = tarfile.open(sys.argv[1])
for member in tar.getmembers():
print ("member_name is: ")
print (member.name)
tar.extract(member.name, "./crachinfo/")
extract_system_report(sys.argv[1])
while extracting the file getting below error:
>> python tar_read.py /nobackup/deepakhe/POLARIS_POJECT_09102019/hackathon_2021/a.tar.gz
member_name is:
/bootflash/.prst_sync/reload_info
Traceback (most recent call last):
File "tar_read.py", line 38, in <module>
extract_system_report(sys.argv[1])
File "tar_read.py", line 10, in extract_system_report
tar.extract(member.name, "./crachinfo/")
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/tarfile.py", line 2052, in extract
numeric_owner=numeric_owner)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/tarfile.py", line 2114, in _extract_member
os.makedirs(upperdirs)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/os.py", line 210, in makedirs
makedirs(head, mode, exist_ok)
File "/auto/pysw/cel8x/python64/3.6.10/lib/python3.6/os.py", line 220, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/bootflash'
I am specifying the folder to extract but still it seems trying to create a folder in the root directory. is this behavior expected? I can extract this tar file fine in file manager. is there a way to handle this in python?
I am specifying the folder to extract but still it seems trying to create a folder in the root directory. is this behavior expected?
Yes. The path provided is just a "current directory", it's not a "jail". The documentation even specifically warns about it:
It is possible that files are created outside of path, e.g. members that have absolute filenames starting with "/" or filenames with two dots "..".
I can extract this tar file fine in file manager. is there a way to handle this in python?
Use extractfile to get a pseudo-file handle on the contents of the archived file, then copy that wherever you want.

RarFile / [WinError 5]: Access Denied

I'm trying to write a script that will automatically extract the files from a rar or zip folder and put them somewhere, so as to make file organization faster. Included are the relevant sections of code:
import shutil
import os
import eyed3
import glob
import zipfile
import rarfile
import unrar
import patoolib
## create zipfile object of the downloaded album and get a tracklist
rarfile.UNRAR_TOOL=r'C:\Users\John\AppData\Local\Programs\Python\Python36-32'
downloads = glob.glob("C:\\Users\\John\\Downloads\\*")
music_zip = max(downloads, key=os.path.getctime)
if os.path.splitext(music_zip)[-1] == '.zip':
music_folder = zipfile.ZipFile(music_zip)
elif os.path.splitext(music_zip)[-1] == '.rar':
music_folder = rarfile.RarFile(music_zip)
print(music_zip)
print(music_folder)
temporary_album_folder = 'C:\\Users\\John\\Downloads\\temporary_album_folder'
if not os.path.exists(temporary_album_folder):
os.makedirs(temporary_album_folder)
# patoolib.extract_archive(music_zip, outdir=temporary_album_folder)
# temp_list = os.listdir(temporary_album_folder)
# tag = eyeD3.load(temp_list[0])
# artist = tag.getArtist()
# album = tag.getAlbum()
# print(os.getcwd())
os.chdir(temporary_album_folder)
music_folder.extractall()
music_folder.close()
print(temporary_album_folder)
When I run this, I expect it to successfully extract the contents of the RAR into a temporary folder in \Downloads. Instead, the error message that I get when I try to run this in the console is:
C:\Users\John\Documents\PythonScripts>music_organizer.py
C:\Users\John\Downloads\d1ctus t3 n3c4r3(5).rar
<rarfile.RarFile object at 0x02C16350>
Traceback (most recent call last):
File "C:\Users\John\Documents\PythonScripts\music_organizer.py", line 40, in <
module>
music_folder.extractall()
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 820, in extractall
self._extract(fnlist, path, pwd)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 885, in _extract
p = custom_popen(cmd)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\site-package
s\rarfile.py", line 2813, in custom_popen
creationflags=creationflags)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\subprocess.p
y", line 707, in __init__
restore_signals, start_new_session)
File "C:\Users\John\AppData\Local\Programs\Python\Python36-32\lib\subprocess.p
y", line 990, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
I know a lot of other people have asked similar questions about WinError 5 and Python, so to address possible common suggestions in advance: I am running the terminal in admin mode, have turned off UAC, have unblocked the folder in question, and have opened full permissions to the folder and sub-folders in question. Does anyone know why this is happening and possible get arounds? Any help much appreciated.
Refer to: Eryksun's comment
It's not a security permission issue. UNRAR_TOOL should be the executable name (optionally the full path) of an unrar program. subprocess.Popen is failing because you're trying to execute the "Python36-32" directory. – eryksun yesterday
The Windows API has some rather useless error code mappings. Internally in the NT API the error in this case is STATUS_FILE_IS_A_DIRECTORY (0xC00000BA), which could not be more obvious, but it gets mapped to ERROR_ACCESS_DENIED (0x0005) by Windows, which misleads you into thinking it's a problem with file or object permissions. – eryksun yesterday

Shutil.move - First run Error, second run success

I looked for similar issues in this forum, but not came up with my situation.
I program python with Eclipse. I am using shutil.move to move some files from one directory to another. When I run the code first, it is giving me the following error. Right after this I run a second try (without changing anything), it finds me the correct files and move to the correct place. Does anyone know what I am doing wrong? If my code is faulty, why it is running in second try without any problem? The source and destination directories are already existing.
Here is the IOError:
Traceback (most recent call last):
File "C:\Users\john\workspace\RC\src\Test.py", line 82, in <module>
shutil.move('C:/RCTemp/' + filename, dest_sonst)
File "C:\Python27\Lib\shutil.py", line 302, in move
copy2(src, real_dst)
File "C:\Python27\Lib\shutil.py", line 130, in copy2
copyfile(src, dst)
File "C:\Python27\Lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: 'C:/RCTemp/Details.xslm'
And this is my code:
import os
import shutil
dest_dkfrontend = 'C:/RhodeCode/11_Detailkonzept_Frontend/'
for filename in source:
if filename.startswith('Details'):
print('Files found ' + filename)
shutil.move("C:/RhodeCodeTemp/" + filename, dest_dkfrontend)
Can anyone help please?

IOError: [Errno 13] Permission denied

I'm getting a permission error when trying to save a screenshot from Sikuli under Windows. The code that's doing the capturing is:
def CaptureScreenshot(self):
resultsDirectory = os.path.join('C','08 May 2013 11 34','myname.png')
screenshot = capture(self.screen)
print(screenshot)
shutil.move(screenshot,self.resultsDirectory)
When I print the screenshot path returned by capture, I get
D:\DOCUME~1\BUNNINGS\LOCALS~1\Temp\sikuli-scr-366782306192033926.png
When I run the code, I get this error:
Traceback (most recent call last):
File "__pyclasspath__/Tests/Tests.py", line 12, in tearDown
File "__pyclasspath__/Scripts/Screen.py", line 39, in CaptureScreenshot
File "C:\jython2.5.3\Lib\shutil.py", line 205, in move
copy2(src,dst)
File "C:\jython2.5.3\Lib\shutil.py", line 96, in copy2
copyfile(src, dst)
File "C:\jython2.5.3\Lib\shutil.py", line 52, in copyfile
fdst = open(dst, 'wb')
IOError: [Errno 13] Permission denied: 'C\\08 May 2013 11 34\\myname.png'
The destination folder exists and myname.png is the new name I am trying to give to the image.
I noticed that the destination folder's properties are set to "read only". Is this causing the issue? I couldn't change the readonly attribute; when I try, it just goes back to readonly.
There seems to be a colon missing after the C in your path. You are now trying to write in a subdirectory 'C' of the current directory.
Try to change the second line into:
resultsDirectory = os.path.join('C:','08 May 2013 11 34','myname.png')
^

Permission Denied error while downloading file from Dropbox in Python

Through the metadata of one folder, I am able to get relative file paths of the files I want to download to my local machine. When I give this path to source code of do_get(), it gives me permission denied error. Here is the code which is supposed to download files and decrypt them but its not able to download the files on the first hand.
#command
def do_decryptFiles(self, from_path, to_path, key):
"""
Decrypt all the files given in the folder and subfolders of from_path
Examples:
Dropbox> decryptFiles '/Photos' 'E:\temp' 'a13223132323232'
"""
folder_metadata = self.api_client.metadata(from_path)
print "metadata:", folder_metadata
for s in folder_metadata['contents']:
if(s['is_dir'] == True):
print "directory:", s['path']
else:
FFPath = s['path']
print FFPath
do_get(self, from_path, to_path)
to_file = open(os.path.abspath(to_path), "wb")
f, metadata = self.api_client.get_file_and_metadata(self.current_path + FFPath)
to_file.write(f.read())
When it calls open(), command line gives me Permission Denied error. Any help would be appreciated.
Traceback (most recent call last):
File "example/cli_client.py", line 397, in <module>
main()
File "example/cli_client.py", line 394, in main
term.cmdloop()
File "C:\Python27\lib\cmd.py", line 142, in cmdloop
stop = self.onecmd(line)
File "C:\Python27\lib\cmd.py", line 219, in onecmd
return func(arg)
File "example/cli_client.py", line 77, in wrapper
return f(self, *args)
File "example/cli_client.py", line 315, in do_decryptFiles
to_file = open(os.path.abspath(to_path), "wb")
IOError: [Errno 13] Permission denied: 'E:\\proto'
Sounds a local directory permissions issue? I had similar problem recently, if it is there are some possible solutions here.
It sounds to me like this isn't a Dropbox API issue, it's a local IO Error.

Categories

Resources