title: Writing to python files - Permission denied
body: <p>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.</p>
  <p>I am using this code:</p>
  <pre class="lang-py prettyprint-override"><code>
  zipfile = 'mode.zip'
  zip_buffer = io.BytesIO(zipfile.get()[&quot;Body&quot;].read())
  zfile = zipfile.ZipFile(zip_buffer)
  zlist = zfile.namelist()
  for f in zlist:
      print('\nfilename', f)
      y = zfile.open(f)
      Archname = 'modeltemp' + &quot;/&quot; + 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 &quot;' + path + '&quot;')
      splits = path.split('/')
      print(&quot;splits&quot;, splits)
      last = splits[-1]
  
      print(last)
  
      if last =='':
          print(&quot;It is a directory&quot;)
      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() 
  
  </code></pre>
  <p>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.</p>
  <p>I am hitting the following error with the same:</p>
  <pre class="lang-py prettyprint-override"><code>
  PermissionError: [Errno 13] Permission denied: 'modeltemp/mode/__init__.py'
  
  </code></pre>
  <p>What should I change? Thanks</p>