What is the glob2 module?
Solution 1
from glob docs
"The glob module finds all the pathnames matching a specified pattern(...)"
i skip the imports import glob2
and
from datetime import datetime
get all the filenames in the directory where filename is any and it is extension is text
filenames = glob2.glob("*.txt")
open new file which name is current datetime in the format as specified in the strftime and open it with write access as variable 'file'
with open(datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f")+".txt", 'w') as file:
for each filenames in found files which names / paths are stored in filenames variable...
for filename in filenames:
with the filename open for read access as f:
with open(filename, "r") as f:
write all content from f into file and add \n to the end (\n = new line)
file.write(f.read() + "\n")
Solution 2
I also saw "glob2"-module used in a kaggle-notebook and researched my own answer in what is the difference to "glob".
All features of "glob2" are in the current included "glob"-implementation of python.
So there is no reason to use "glob2" anymore. As for what glob does in general, BlueTomato already provided a nice link and description.
Related videos on Youtube
Tomerikoo
Updated on June 04, 2022Comments
-
Tomerikoo about 2 years
import glob2 from datetime import datetime filenames = glob2.glob("*.txt") with open(datetime.now().strftime("%Y-%m-%d-%H-%M-%S-%f")+".txt", 'w') as file: for filename in filenames: with open(filename, "r") as f: file.write(f.read() + "\n")
I was working in python and came across this name glob, googled it and couldn't find any answer, what does glob do, why is it used for?
-
Jean-François Fabre over 6 yearsif it works, it scans all text files from curent dir and concatenate their contents on a big, dated file.
-
Jean-François Fabre over 6 yearsif you ask me, the flaw is that running this program again uses the output from prior execution since the extension & dir are the same for input & output
-
Matthias over 6 years
glob2
is an external library. -
Admin over 6 yearsI am new to python i really dont have any idea about glob2, filenames = glob2.glob("*.txt") what does the above code does.. can someone explain this line by line...
-
Wolverine313 over 5 yearshmmm... I see someone is doing the python mega course. ;)
-
-
Gigi over 3 yearsIs this still the case / is glob2 outdated?
-
Stefan over 3 yearsThe last version is from June 2019 ... I would go for the python-included "glob".