glob.glob vs Path.glob
Python glob multiple filetypes - Stack Overflow
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")
Why does this python's glob.glob method give an output when the parent directories do not exist?
For listing a directory contents why would I choose
Path('dirname').glob('*')instead of
glob.glob('dirname/*')Maybe there is a better way, but how about:
import glob
types = ('*.pdf', '*.cpp') # the tuple of file types
files_grabbed = []
for files in types:
files_grabbed.extend(glob.glob(files))
# files_grabbed is the list of pdf and cpp files
Perhaps there is another way, so wait in case someone else comes up with a better answer.
glob returns a list: why not just run it multiple times and concatenate the results?
from glob import glob
project_files = glob('*.txt') + glob('*.mdown') + glob('*.markdown')
I am creating a program that requires scanning a directory for pdf files.
Do both functions have significant difference in processing speed especially when scanning a directory with a particular extension format?
Here are my two codes on how I use them both:
os.chdir("./")
for file in glob.glob("*.pdf"):
convert_file_to_image(file)And
for file in os.listdir("./"):
if file.endswith(".pdf"):
convert_file_to_image(file))I believe when using the listdir() function can create more bandwidth requirement as I have a condition where it checks when the file ends in a certain format, compared with glob() wherein it accepts an argument before starting the loop, however I need to specify a chdir() before it starts.
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")
Use pathlib. It was introduced into a standard library for a reason. Also it's based on scandir, so it will be the most efficient.