0

I am trying to extract a zipped folder into a temp folder. I am reading the zipped folder, file by file, create the same directory in the temp folder and then writing the content in the files.

I am using this code:


zipfile = 'mode.zip'
zip_buffer = io.BytesIO(zipfile.get()["Body"].read())
zfile = zipfile.ZipFile(zip_buffer)
zlist = zfile.namelist()
for f in zlist:
    print('\nfilename', f)
    y = zfile.open(f)
    Archname = 'modeltemp' + "/" + f
    x = io.BytesIO(y.read())
    S3.put_object(Body = x, Bucket = bucketname, Key = Archname) # for saving in the s3 folder 
    writefile(Archname,x) # for saving in the temp folder 

def writefile(path,file):
    os.system('mkdir -p "' + path + '"')
    splits = path.split('/')
    print("splits", splits)
    last = splits[-1]

    print(last)

    if last =='':
        print("It is a directory")
    else:
        
        fl = open(path, 'w+')
        if (type(file) == str):
            fl.write(file)
        else:
            if (type(file) == io.BytesIO):
                body = file.read()
            else:
                body = file.getvalue()
            f.write(str(body))
        fl.close() 

This code is creating the required directories. When its folder it skips the writing to the file but when its a file it tries to open the file in write mode.

I am hitting the following error with the same:


PermissionError: [Errno 13] Permission denied: 'modeltemp/mode/__init__.py'

What should I change? Thanks

zsh_18
  • 1,012
  • 1
  • 11
  • 29
  • Do you have the files open when you are trying to write to it? Sometimes, when the file you are trying to modify is open, you get hit with a permission error. – patrick7 Feb 10 '22 at 00:23
  • Hi @patrick7- This the whole code which is creating the file. I have not personally opened the file anywhere before the write function happens. Also I can not see in the code anywhere of it being opened. The path is getting created and just then it should open the file and write. Can you see anywhere I am missing out on anything ?Thanks – zsh_18 Feb 10 '22 at 00:25
  • The line fl=open(path, 'w+'), is path a directory or a file? Sometimes if you try to open with the write flag on a directory, it will give you the permission denied. Can you double check that is not a directory? – patrick7 Feb 10 '22 at 00:41
  • Perhaps this can be of use https://stackoverflow.com/questions/36434764/permissionerror-errno-13-permission-denied – patrick7 Feb 10 '22 at 00:48

0 Answers0