I am going over glob and iglob. iblog doesn’t fill up your memory with a list. I am also using os.getcwd or get current working directory and os.chdir for change directory. glob creates a list. iglob brings down one directory/file at a time. I went down to the cli directory to keep the output smaller.
1.) Show current directory
2.) Change directory down to cli
3.) first glob command prints out the list.
4.) I take the list and parse it with a loop into files.
5.) I take the same command with iglob
6.) I print the iglob with a loop
7.) I change it to only show .py files.
#!/usr/bin/env/python3 def main(): from glob import glob from glob import iglob import os cwd = os.getcwd() print("current directory", os.getcwd()) new_path = "venv/lib/python3.8/site-packages/pip/_internal/cli" print('changed path to {}'.format(new_path)) cwd = os.chdir('venv/lib/python3.8/site-packages/pip/_internal/cli') print("current directory", os.getcwd()) print("") files = glob('./**/*.*', recursive=True) print("files in a list using glob, uses up memory") print(files) print("") print('print glob files in for loop') for i in files: print('{}'.format(i)) files = iglob('./**/*.*', recursive=True) print("") print('print iglob files in for loop') for i in files: print('{}'.format(i)) files = iglob('./**/*.py', recursive=True) print("") print('print iglob .py files in for loop') for i in files: print('{}'.format(i)) if __name__ == '__main__': main()
/home/michael/PycharmProjects/glob/venv/bin/python /home/michael/PycharmProjects/glob/main.py current directory /home/michael/PycharmProjects/glob changed path to venv/lib/python3.8/site-packages/pip/_internal/cli current directory /home/michael/PycharmProjects/glob/venv/lib/python3.8/site-packages/pip/_internal/cli files in a list using glob, uses up memory ['./__init__.py', './main_parser.py', './autocompletion.py', './progress_bars.py', './parser.py', './base_command.py', './command_context.py', './cmdoptions.py', './status_codes.py', './main.py', './req_command.py', './spinners.py', './__pycache__/progress_bars.cpython-38.pyc', './__pycache__/status_codes.cpython-38.pyc', './__pycache__/command_context.cpython-38.pyc', './__pycache__/cmdoptions.cpython-38.pyc', './__pycache__/spinners.cpython-38.pyc', './__pycache__/parser.cpython-38.pyc', './__pycache__/base_command.cpython-38.pyc', './__pycache__/req_command.cpython-38.pyc', './__pycache__/autocompletion.cpython-38.pyc', './__pycache__/__init__.cpython-38.pyc', './__pycache__/main_parser.cpython-38.pyc', './__pycache__/main.cpython-38.pyc'] print glob files in for loop ./__init__.py ./main_parser.py ./autocompletion.py ./progress_bars.py ./parser.py ./base_command.py ./command_context.py ./cmdoptions.py ./status_codes.py ./main.py ./req_command.py ./spinners.py ./__pycache__/progress_bars.cpython-38.pyc ./__pycache__/status_codes.cpython-38.pyc ./__pycache__/command_context.cpython-38.pyc ./__pycache__/cmdoptions.cpython-38.pyc ./__pycache__/spinners.cpython-38.pyc ./__pycache__/parser.cpython-38.pyc ./__pycache__/base_command.cpython-38.pyc ./__pycache__/req_command.cpython-38.pyc ./__pycache__/autocompletion.cpython-38.pyc ./__pycache__/__init__.cpython-38.pyc ./__pycache__/main_parser.cpython-38.pyc ./__pycache__/main.cpython-38.pyc print iglob files in for loop ./__init__.py ./main_parser.py ./autocompletion.py ./progress_bars.py ./parser.py ./base_command.py ./command_context.py ./cmdoptions.py ./status_codes.py ./main.py ./req_command.py ./spinners.py ./__pycache__/progress_bars.cpython-38.pyc ./__pycache__/status_codes.cpython-38.pyc ./__pycache__/command_context.cpython-38.pyc ./__pycache__/cmdoptions.cpython-38.pyc ./__pycache__/spinners.cpython-38.pyc ./__pycache__/parser.cpython-38.pyc ./__pycache__/base_command.cpython-38.pyc ./__pycache__/req_command.cpython-38.pyc ./__pycache__/autocompletion.cpython-38.pyc ./__pycache__/__init__.cpython-38.pyc ./__pycache__/main_parser.cpython-38.pyc ./__pycache__/main.cpython-38.pyc print iglob .py files in for loop ./__init__.py ./main_parser.py ./autocompletion.py ./progress_bars.py ./parser.py ./base_command.py ./command_context.py ./cmdoptions.py ./status_codes.py ./main.py ./req_command.py ./spinners.py Process finished with exit code 0