You state that the following code creates a file named "Archive" in the archive folder:
for file in copyPath.rglob("*V1.2*"):
shutil.move(file,Path(file.parent,"Archive"))
shutil.move only moves your files into a directory if the directory already exists. Otherwise, it'll move (rename) your file. I guess that Path(file.parent, "Archive") is not a directory, so every file matching the condition is moved into a file called "Archive", each file overwriting the previous one.
The fix should be simple:
for file in copyPath.rglob("*V1.2*"):
target = Path(file.parent, "Archive")
if target.is_file():
raise ValueError(f"Invalid directory: {target} is a file!") # safety check to avoid overwriting files and data loss
if not target.is_dir():
target.mkdir(parents=True) # <- this should solve your problem
shutil.move(file, target)
Answer from Green 绿色 on Stack Overflowglob and rglob can be slow for large directories or lots of files
glob.glob vs Path.glob
Why you should be using pathlib
Which is much more efficient, glob.glob() or os.listdir()?
There's going to be almost no difference. Under the covers, glob is essentially doing the same thing as your listdir code (ie calling listdir and then checking if the paths match a particular pattern). In fact, it's actually doing a bit more work since it's more general (ie. handles more types of patterns than just "end of file"), so it's actually going to be the slower one.
However the difference is going to be pretty much undetectable unless you're looking at directories with millions of files (and even then is going to be pretty negligible). Computers are fast, and this kind of check is going to take a tiny amount of time - the time to actually access the disk is always going to completely dwarf it - trying to optimise it is like trying to reduce the time it takes a train door to open in an effort to cut down the time a 10 minute journey takes. Sure: maybe you could cut half a second off the time with a faster door, but it's not going to be noticable next to the travel time.
however I need to specify a chdir() before it starts
Not at all - you can include the path as part of your pattern. Ie glob.glob("path/to/file/*.pdf")
For listing a directory contents why would I choose
Path('dirname').glob('*')instead of
glob.glob('dirname/*')