Reputation: 15
I have functions using glob to identify files for removal. In the first function, the recursive toggle works as intended. In the second function, the recursive toggle is somewhat working... Here's a sample of my code below.
for item in glob.glob(myfile_path + '/**', recursive=self.recrsv.get()):
if item.endswith(".mesg"):
os.remove(os.path.join(myfile_path, item))
This function works as intended.
for junk in glob.glob(myfile_path + '/**' + "/*.*[0-9][0-9][0-9][0-9]", recursive=self.recrsv.get()):
os.remove(os.path.join(myfile_path, junk))
This function somewhat works. Whether recursive
is set to True
or False
, it still goes into the first level of subdirectories and deletes files *.*[0-9][0-9][0-9][0-9]
. It does not go into the second level of subdirectories unless recursive
is set to True
.
Note: Recursive is set to a tk.BooleanVar()
named self.recrsv
used in a Checkbutton in a tkinter widget.
Thank you in advance.
Upvotes: 0
Views: 206
Reputation: 15
I've made a workaround for my issue using an if-statement and repeating the glob under it.
for junk in glob.glob(myfile_path + "/*.*[0-9][0-9][0-9][0-9]"):
os.remove(os.path.join(myfile_path, junk))
#The above code will delete the specified junk file within myfile_path.
if self.recrsv.get()==1:
for junk in glob.glob(myfile_path + '/**' + "/*.*[0-9][0-9][0-9][0-9]",
recursive=self.recrsv.get()):
os.remove(os.path.join(myfile_path, junk)
#The above code checks the recursive variable, and if true, will recursively delete the junk files.
If I do not use the if-statement and simply rely on glob's own recursive check, the junk file will be removed from the first level of subdirectories even if recursive=False
.
I can probably clean up the code some more to be a bit more if-statement-intuitive, but the problem has been solved. Thank you, @Szellem and @user2357112 for your efforts.
Upvotes: 0
Reputation: 514
https://docs.python.org/3/library/glob.html
"If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is followed by an os.sep, only directories and subdirectories match."
So **
in <myfile_path>/**/*.*[0-9][0-9][0-9][0-9]
matches exactly one level of subdirectories when recursive is set to false. Because /
is os.sep
I assume.
Upvotes: 1
Reputation: 281683
With recursive=False
,
myfile_path + '/**' + "/*.*[0-9][0-9][0-9][0-9]"
is equivalent to
myfile_path + '/*' + "/*.*[0-9][0-9][0-9][0-9]"
or
myfile_path + '/*/*.*[0-9][0-9][0-9][0-9]"
However, even with recursive=False
, this pattern explicitly says to search through subdirectories of myfile_path
, not through the contents of myfile_path
itself. A pattern saying to search through myfile_path
would be
myfile_path + '/*.*[0-9][0-9][0-9][0-9]"
(Also, building a glob this way is dangerous, because myfile_path
could contain glob metacharacters. Remember to use glob.escape
on components you want to match literally, or pick a search mechanism that doesn't need escaping.)
Upvotes: 1