14

My Folder Structure looks like this:

- 95000
- 95002
- 95009
- AR_95000.pdf
- AR_95002.pdf
- AR_95009.pdf
- BS_95000.pdf
- BS_95002.pdf
- BS_95009.pdf

[Note 95000, 95002, 95009 are folders]


My goal is to move files AR_95000.pdf and BS_95000.pdf to the folder named 95000, then AR_95002.pdf and BS_95002.pdf to the folder named 95002 and so on.

The PDFs are reports generated by system and thus I can not control the naming.

1

1 Answer 1

35

Using pathlib this task becomes super easy:

from pathlib import Path

root = Path("/path/to/your/root/dir")

for file in root.glob("*.pdf"):
    folder_name = file.stem.rpartition("_")[-1]
    file.rename(root / folder_name / file.name)

As you can see, one main advantage of pathlib over os/shutil (in this case) is the interface Path objects provide directly to os-like functions. This way the actual copying (rename()) is done directly as an instance method.


References:

2
  • This worked like as you mentioned, its fast and all files moved inside respective folders. I was fiddling with shutil but no avail and constantly getting errors...having said that if you get the time can you mention how above code works. I am getting some sence that you split the files by underscore[ _ ] and then what is next part of code file.stem.rsplit("_", 1)[-1]
    – Drp RD
    Aug 19, 2020 at 14:18
  • 2
    @DrpRD if you are familiar with the split method, then rsplit basically does the same, but once you give it the extra maxsplit argument - the splits are counted from the right. This way because we only want the last part after _ (assuming AB_CD_9500 is also an option) we only split once from the right - rsplit('_', 1). This returns ['AR', '95000'] and by indexing with [-1] we take the last element (95000)
    – Tomerikoo
    Aug 19, 2020 at 15:16

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.