'Writing to python files - Permission denied

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source