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.glob vs Path.glob
glob.glob returns meager ol' strings whereas Path.glob returns you easy to work with path objects. More on reddit.com
glob and rglob can be slow for large directories or lots of files
Hello! While gleefully using cloudpathlib, I needed a recursive iteration of files in a directory. This directory is large (6238 files), so my first approach - a recursive iterdir() + is_file() - t... More on github.com
Path.rglob performance issues in deeply nested directories compared to glob.glob(recursive=True)
With a 1000-deep nested directory, glob.glob and Path.glob both took under 1 second. Path.rglob took close to 1.5 minutes. More on github.com
purpose of .glob(r'**/*.jpg') and Path module?
You should read the documentation of pathlib https://docs.python.org/3/library/pathlib.html#basic-use Pathlib is representation of path to files and directories in python. glob is a method to search pattern in a path See the pattern language documentation https://docs.python.org/3/library/pathlib.html#pathlib-pattern-language So train_dir is actually reference to a folder named train and it's under path folder. If path is /xyz Then train_dir is /xyz/train More on reddit.com
Real Python
realpython.com › lessons › listing-conditionally-rglob
Listing Conditionally Using .rglob() (Video) – Real Python
In this example, you can see the same items as before are listed, such as realpython, rename_files.py, and request.py. This shows that the call to glob method with pattern two asterisks, forward slash R and a single asterisk is equivalent to the .rglob() method with the R and single asterisk.
Published: June 11, 2024
Reddit
reddit.com › r/learnpython › glob.glob vs path.glob
r/learnpython on Reddit: glob.glob vs Path.glob
January 8, 2023 -
For listing a directory contents why would I choose
Path('dirname').glob('*')instead of
glob.glob('dirname/*')GitHub
github.com › drivendataorg › cloudpathlib › issues › 274
glob and rglob can be slow for large directories or lots of files · Issue #274 · drivendataorg/cloudpathlib
October 7, 2022 - In my mind, shortcutting these common "list everything" recursive calls to glob and rglob to a much faster version with _list_dir may be the best path forward. No new API, direct speedups, relatively simple implementation. The downside is the runtime different between a glob with more complex logic and a simple recursive one may be substantial (which could be confusing).
GitHub
facelessuser.github.io › wcmatch › pathlib
wcmatch.pathlib - Wildcard Match Documentation
match mimics Python's pathlib version of match. Python's match uses a right to left evaluation that behaves like rglob but as a matcher instead of a globbing function. Wildcard Match emulates this behavior as well. What this means is that when provided with a path some/path/name, the patterns ...
Computer Science Atlas
csatlas.com › python-list-directory
Python 3: List the Contents of a Directory, Including Recursively — Computer Science Atlas
March 27, 2021 - Since rglob is the same as calling glob with **/, we could also just use glob to achieve the same result: from pathlib import Path for p in Path( '.' ).glob( '**/*.html' ): print( p ) On any version of Python 3, we can use the built-in os library to list directory contents.
Switowski
switowski.com › blog › pathlib
Pathlib for Path Manipulations - Sebastian Witowski
July 17, 2023 - def os_walk_files(): python_files = [] for root, dirs, files in os.walk("."): for filename in files: if filename.endswith(".py"): python_files.append(root + filename) return python_files · But it's much easier to use the glob module instead. That way we just need one line of code: import glob def glob_find_files(): glob.glob("./**/*.py", recursive=True) pathlib comes with a similar function called rglob().
PyPI
pypi.org › project › rglob
rglob · PyPI
Lightweight recursive search for ... — globs, content grep, stat/dup helpers, JSON-stable outputs, typed agent API, optional MCP server. ... ____ ____ _ _ | _ \ / ___| | | ___ | |__ | |_) | | _ | |/ _ \| '_ \ | _ <| |_| | | | (_) | |_) | |_| \_\\____| |_|\___/|_.__/ / / / / / Lightweight recursive search for Python, CLIs, and coding agents. Modern Python has pathlib.Path.rglob, and external ...
Astral
docs.astral.sh › ruff › rules › glob
glob (PTH207) | Ruff - Astral Docs
import glob import os glob.glob(os.path.join("my_path", "requirements*.txt")) ... While using pathlib can improve the readability and type safety of your code, it can be less performant than the lower-level alternatives that work directly with strings, especially on older versions of Python.
Chris-piekarski
chris-piekarski.github.io › python-rglob
rglob
Lightweight recursive glob helpers for Python — find files, count lines, sum sizes. This is a small labor-of-love package. Modern Python has pathlib.Path.rglob and tools like fd/ripgrep exist; rglob is small, easy to read, and ships with friendly helpers for the two things you usually want next: count lines and sum sizes.
Real Python
realpython.com › lessons › listing-conditionally-glob
Listing Conditionally Using .glob() (Video) – Real Python
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas. ... 00:05 The .glob() method of a path object behaves in much the same way as the .rglob method, but does not perform recursion by default.
Published: June 11, 2024
Reddit
reddit.com › r/learnpython › purpose of .glob(r'**/*.jpg') and path module?
r/learnpython on Reddit: purpose of .glob(r'**/*.jpg') and Path module?
November 15, 2025 -
Question 1: What is the explaination of this expression r'**/*.jpg' like what **/* is showing? what is r?
Question 2: How Path module works and what is stored in train_dir? an object or something else?
from pathlib import Path import os.path # Create list with the filepaths for training and testing train_dir = Path(os.path.join(path,'train')) train_filepaths = list(train_dir.glob(r'**/*.jpg'))
Top answer 1 of 5
12
You should read the documentation of pathlib https://docs.python.org/3/library/pathlib.html#basic-use Pathlib is representation of path to files and directories in python. glob is a method to search pattern in a path See the pattern language documentation https://docs.python.org/3/library/pathlib.html#pathlib-pattern-language So train_dir is actually reference to a folder named train and it's under path folder. If path is /xyz Then train_dir is /xyz/train
2 of 5
5
There isn't really any point in mixing pathlib.Path and os.path like this, so it would be better to have: training_dir = path / 'train' You don't show where path is defined, but just make it so that it's defined as a Path object. This object is just a way of storing file paths that has a load of useful methods. glob searches based on patterns, you are searching training_dir and the first part of the pattern (**/) is saying to search all of the subdirectories in training_dir. The second part (*.jpg) is saying to find all files ending with .jpg. An r-string is a raw string, meaning special characters are no longer treated as special characters and instead as just part of the string. You should have a look at the documentation of pathlib and glob and then play around with it.
DEV Community
dev.to › guionardo › fast-folder-iteration-in-python-3g1f
Fast folder iteration in python - DEV Community
January 21, 2022 - import pathlib folder='some_folder/another_folder' for path in pathlib.Path(folder).rglob('*'): if path.is_file(): yield str(path) ... Creating sample files + Creating folder ./test_files + Creating files LEVELS=6 FOLDER_COUNT=6 FILE_COUNT_BY_FOLDER=20 + Created 933120 files in 0:02:08.928680 * Running iterators: GlobFolderIterator IGlobFolderIterator OSWalkFolderIterator ScanDirIterator PathLibFolderIterator