title: How to handle upload File in Plone using plone.api create and NamedBlobFile
body: <p>Dears,</p>
  
  <p>I have been create a script to read a txt and do a upload in Plone site (Plone 4.3.10).
  The script is here:
  <a href="https://stackoverflow.com/questions/38351633/script-python-using-plone-api-to-create-file-appear-error-wrongtype-when-set-a-f">Script Python using plone.api to create File appear error WrongType when set a file</a></p>
  
  <p>I use a tip described by MrTango:
  <a href="https://stackoverflow.com/questions/29408529/save-file-to-plone-site-using-python/29410325#29410325">Save file to plone site using python</a></p>
  
  <p>My difficulty is attach the file into a new item created in begin of FOR, specifically in the following passage:</p>
  
  <pre><code>    file_obj.file = NamedBlobFile(
          data=open(pdf_path, 'r').read(),
          contentType='application/pdf',
          filename=unicode(file_obj.id, 'utf-8'),
      )
  </code></pre>
  
  <p>The parameter "data", receave file PDF from filesystem, but, don't set up file in a new object, newly created.</p>
  
  <p>Thank You for your attention!</p>
  
  <p><strong>[UPDATE]</strong></p>
  
  <p>Using pdb, I saw some outputs, data, contentType n filename is apparently setted up correct.</p>
  
  <p>So, Where I went wrong?
  Or what kind type is input of data?
  <em>I ain't expert python programmer... like you see...</em> If someone used plone.api and upload pdf to plone, how you did it?</p>
  
  <pre><code> 36                             pdf_path,
   37                             filename=unicode(file_obj.id, 'utf-8'),
   38                     )
   39                     print('\n \n Criado: '+row['NDOPROCESSO']+'.')
   40                     transaction.commit()
   41
   42                     pdf_file.close()
   43  -&gt;                 break
   44     csvfile.close()
   45
      (Pdb) file_obj.file.data
          'INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF'
      (Pdb) file_obj.file.contentType
          'application/pdf'
      (Pdb) file_obj.file.filename
          u'processoinmeq-al.pdf'
  </code></pre>
  
  <p><strong>[UPDATE 2]</strong></p>
  
  <p>@Mathias, first thks, you're great!</p>
  
  <p>My O.S. is:</p>
  
  <pre><code>jaf@ocs:~/plone4310/zinstance$ uname -a
  Linux ocs 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u6 (2015-11-09) x86_64 GNU/Linux
  </code></pre>
  
  <p>Look my attempt:</p>
  
  <pre><code>jaf@ocs:~/plone4310/zinstance$ bin/instance -O a debug
  Starting debugger (the name "app" is bound to the top-level Zope object)
  
  &gt;&gt;&gt; plone = app.a
  &gt;&gt;&gt; plone
  &lt;PloneSite at /a&gt;
  &gt;&gt;&gt; from zope.component.hooks import setSite
  &gt;&gt;&gt; setSite(plone)
  &gt;&gt;&gt; from plone import api
  &gt;&gt;&gt; pdfpath = 'INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF' 
  #I tried too with a full path, like you see in image print below
  #'/home/jaf/plone4310/zinstance/INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF'
  &gt;&gt;&gt; pdfpath
  'INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF'
  &gt;&gt;&gt; obj = api.content.create(type='File', title='a file', container=plone)
  &gt;&gt;&gt; obj
  &lt;ATFile at /a/a-file&gt; #unique difference, between you and me, is ATFile not File.
  &gt;&gt;&gt; obj.id
  'a-file'
  &gt;&gt;&gt; file_ = plone.get(obj.id)
  &gt;&gt;&gt; file_
  &lt;ATFile at /a/a-file&gt;
  &gt;&gt;&gt; file_ = plone.get('a-file')
  &gt;&gt;&gt; file_
  &lt;ATFile at /a/a-file&gt;
  &gt;&gt;&gt; from plone.namedfile.file import NamedFile
  &gt;&gt;&gt; pdf = open(pdfpath, 'r')
  &gt;&gt;&gt; pdf
  &lt;open file 'INMEQ/PastaGeral/PROCESSOINMEQ-AL.PDF', mode 'r' at 0x7f41b7b48030&gt;
  &gt;&gt;&gt; file_.file = NamedFile(data=pdf, filename=unicode(obj.id, 'utf-8'), contentType='application/pdf')
  &gt;&gt;&gt; file_.file
  &lt;plone.namedfile.file.NamedFile object at 0x7f41b7b42500&gt;
  &gt;&gt;&gt; import transaction
  &gt;&gt;&gt; transaction.commit()
  &gt;&gt;&gt; file_.file
  &lt;plone.namedfile.file.NamedFile object at 0x7f41b7b42500&gt;
  &gt;&gt;&gt;
  </code></pre>
  
  <p>The local of files:
  <a href="https://i.stack.imgur.com/wXeEp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wXeEp.png" alt="Image Print"></a></p>
  
  <p>And after transaction.commit(), we have it.</p>
  
  <p><a href="https://i.stack.imgur.com/8YtGX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8YtGX.png" alt="Plone Site with empty File after commit."></a></p>
  
  <p><strong>[UPDATE 3 - AND WORKS]</strong></p>
  
  <p>With the SUPER HELP of @Mathias, the script python for upload <code>File</code> Archetypes from txt and separation with semicolon.</p>
  
  <pre><code>from zope.site.hooks import setSite
  from plone import api
  import transaction
  import csv
  import os
  
  local_path = '/path_to_plone_instace/plone4310/zinstance'
  scan_path = 'path_with_txt_and_PDFs'
  geral_path = 'folder_with_pdf_only'
  txt_name = 'file_with_content.txt'
  plone_site = 'plone_site'
  plone_site_pasta = 'folder_upload_in_plone'
  
  portal = app[plone_site]
  setSite(portal)
  container = portal[plone_site_pasta]
  
  with open(os.path.join(local_path, scan_path, txt_name), 'rb') as csvfile:
      reader = csv.DictReader(csvfile, delimiter=';', quotechar='|')
      for row in reader:
          pdf_id = str(row['PDF_ID'])
          pdf_file = open(os.path.join(local_path, scan_path, geral_path, str(pdf_id)), 'r')
  
          file_obj = api.content.create(
              container=container,
              type='File',
              title=str(row['PDF_TITLE']),
              description=str(row['PDF_DESCRIPTION']),
              safe_id=True,
              excludeFromNav=True
          )
  
          file_ = container.get(file_obj.id)
  
          file_.setFile(pdf_file)
  
          if int(file_obj.getFile().size()) &lt;= 0:
              print str(file_obj.id) + ', Empty FILE!!! size: ' + str(file_obj.getFile().size())
  
          else:
              print str(file_obj.title) + ', success created, with size: ' + str(file_obj.getFile().size())
  
              transaction.commit()
  
  csvfile.close()
  </code></pre>