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