title: Python: Recreate file path with shorter name(s)?
body: <p>D:\MyDir contains a huge number of directories with or without subdirectories which may or may not contain one or more files.</p>
  <p>Some of them have very long name +/- very long name for the subdirectories and/or file(s) too which leads to a huge path name for that file.</p>
  <p>e.g.</p>
  <pre><code>D:\MyDir:
  &quot;Very large directory name&quot;\
      &quot;Very large directory name&quot;\ 
  #OPTIONAL
  #other or the same name repeating one or more times
          &quot;normal dir&quot;\ #optional
               Long_or_normal_name_MyFile.html and Long_or_normal_name_MyFile.html and so on...
  </code></pre>
  <p>I want to create a Python script that:</p>
  <ol>
  <li>Move the file(s) to the first directory (base directory) named &quot;Very large directory name&quot;.</li>
  <li>Rename the file with a shorter name.</li>
  <li>Rename the base directory with a shorter name and after that remove empty directories left behind.</li>
  </ol>
  <p>To move a file or a folder I can do that with shutil.move().</p>
  <p>With this code I list all the files from a directory and store them into a list:</p>
  <pre><code>from os import listdir
  from os.path import isfile, join
  onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
  </code></pre>
  <p>And this line will list only first level directory from D:\MyDir and store them into a list:</p>
  <pre><code>[ name for name in os.listdir(thedir) if os.path.isdir(os.path.join(thedir, name)) ]
  </code></pre>
  <p>But how to do it further in order to do what I need to do?</p>
  <p>Forgot to say that D:\MyDir contains also directories and files that don't need to be renamed and/or moved.</p>
  <p>Thank you in advance!</p>