title: Rename files using sub folder names
body: <p>My folder structure is as follows</p>
  
  <pre><code>Folder A(Parent folder)
  
    Folder B(Sub folder)
  
     1.jpg
     2.jpg
     3.jpg
     4.jpg
  
    Folder B1(Sub folder)
  
     1.jpg
     2.jpg
     3.jpg
     4.jpg
  
    .....
  
    Folder Bn(Sub folder)
  
     1.jpg
     2.jpg
     ...jpg
  </code></pre>
  
  <p>How can I rename .jpeg filenames with sub folder names, like this B-1.jpg, B-2.jpg.... and B1-1.jpg, B1-2.jpg... and Bn-1.jpg, Bn-2.jpg ....</p>
  
  <p>My code so far (I have taken the code from this <a href="https://stackoverflow.com/questions/26651712/count-and-print-number-of-files-in-subfolders-using-python">question</a>:</p>
  
  <pre><code>import os
  path = os.getcwd()
  # get all directories
  folders = ([name for name in os.listdir(path)
          if os.path.isdir(os.path.join(path, name))])
  for folder in folders:
    for fname in os.listdir(os.path.join(path,folder)):
      new_fname = os.path.basename(os.path.join(path,folder)) + fname.split('.') **# I think error is in here**
      os.rename(os.path.join(path, fname), os.path.join(path, new_fname))
  </code></pre>
  
  <p>when I run the code, it just shows:</p>
  
  <pre><code>Traceback (most recent call last):
    File "&lt;stdin&gt;", line 3, in &lt;module&gt;
  TypeError: cannot concatenate 'str' and 'list' objects
  </code></pre>
  
  <p>I am novice in python, please help and make this complete and perfect, please.</p>