title: How do I dynamically create a variable name in a loop to assign to a file name in python 3
body: <p>I'm still relatively new to programming and Python. But I am sure this must be possible but my searches are not turning up what I'm looking for.</p>
  <p>In my current directory, I have 6 PDF files that I wish to read in via the loop below.</p>
  <p>What I would like to do is open each of the PDF's with a new variable name, as you can see it is imaginatively called <code>pdf[1-6]File.pdf</code>.</p>
  <p>I can list the files in the console and pull them via the code when I stick breaks in to stop it executing but I can't for the life of me work out how to create the variable name. I thought something like &quot;pdf&quot; + str(i) + &quot;File&quot; would have worked but I'm missing something.</p>
  <p>Code is below - not complete but enough so you get what I'm looking at:</p>
  <pre><code>#Open the PDF files in the current directory for
  #reading in binary mode
  def opensource():
      listOfFiles = os.listdir('.')
      pattern = &quot;*.pdf&quot;
      for entry in listOfFiles:
          if fnmatch.fnmatch(entry, pattern):
              # Works to here perfectly
              for i in range(len(entry)):
                  # print(len(entry))
                  # Trying to create the variable name with 
                  # an incremental numeral in the file name
                  &quot;pdf&quot; + i + &quot;File&quot; = open(entry, 'rb')
  </code></pre>
  <p>This bit below is how I'm currently doing it and its a pain in the backside. I'm sure it can be done programmatically</p>
  <pre><code>#This is the old way. Monolithic and horrid 
  #Open the files that have to be merged one by one
  pdf1File = open('file1.pdf', 'rb')
  pdf2File = open('file2.pdf', 'rb')
  pdf3File = open('file3.pdf', 'rb')
  pdf4File = open('file4.pdf', 'rb')
  pdf5File = open('file5.pdf', 'rb')
  pdf6File = open('file6.pdf', 'rb')
  </code></pre>
  <p>All help gratefully received.</p>
  <p>Thanks</p>