title: How to save files to a directory and append those files to a list in Python?
body: <p><strong>Scenario:</strong></p>
  
  <p>I want to check whether if a directory contains a certain '.png' image file. If so, this image file along with all the other files (with png extension only) gets stored in a different directory. (The solution I am looking for should work in all OS platforms i.e Windows, Unix, etc.) and in a remote server i.e (FTP etc.)</p>
  
  <p>I have tried the following code below:</p>
  
  <pre><code>import os, sys
  import shutil
  import pathlib
  import glob
  
  def search():
      image_file = 'picture.png'
      try:
          arr = []  #List will be used to append all the files in a particular directory.
          directory = pathlib.Path("collection") #checks if the collection directory exists.
  
          files = []
          #need to convert the PosixPath (directory) to a string.
          [files.extend(glob.glob(str(directory) + "/**/*.png", recursive = True))]
  
          res = [img for img in files if(img in image_file)]  #checks if the image is within the list of files i.e 'picture.png' == 'collection\\picture.png'
  
          if str(bool(res)):  #If True...proceed
              print("Image is available in image upload storage directory")
  
              for file in files:
                  transfer_file = str(file)
                  shutil.copy(file, 'PNG_files/')   #send all the files to a different directory i.e 'PNG_files' by using the shutil module.
                  arr.append(transfer_file)
              return arr
          else:
              print("image not found in directory")
      except OSError as e:
          return e.errno
  
  result = search() #result should return the 'arr' list. This list should contain png images only.
  </code></pre>
  
  <p><strong>However, during execution, the For loop is not getting executed. Which means:</strong> </p>
  
  <ol>
  <li>The image files are not stored in the 'PNG_files' directory.</li>
  <li>The images are not getting appended in the 'arr' list.</li>
  </ol>
  
  <p>The code above the For loop worked as expected. Can anyone explain to me what went wrong? </p>