AnsweredAssumed Answered

Removing Characters from a Folder Name

Question asked by pjgardiner on Dec 9, 2019
Latest reply on Dec 10, 2019 by pjgardiner

So I originally had a query relating to moving a pdf map to a separate folder which is named with the pdf name.


Johannes Bierer provided the code below (which works perfectly):


import glob, os, shutil
from shutil import copyfile

inPath = r"yourPath"
outPath = r"yourPath"

for file in glob.glob(os.path.join(inPath, "*.pdf")):

    outFolder = os.path.basename(file[:-4])

    if os.path.isdir(os.path.join(outPath, outFolder)):
        shutil.rmtree(os.path.join(outPath, outFolder))
    outDir = os.mkdir(os.path.join(outPath, outFolder))
    outDir1 = os.path.join(outPath, outFolder)
    outFile = os.path.join(outDir1, os.path.basename(file))
    copyfile(file, outFile)



My follow up query to this is what should be added to the above code in order that the first 12 characters of the file name are removed when naming the folder?


E.g. the file name is 'Species_Map 12_HH_132'. I would like the folder to be called '12_HH_132'.


Wasn't sure whether this should be appended to the original question or not.