The glob module

The glob module (https://docs.python.org/3/library/glob.html) enables identifying files of a specific extension or files that have a specific pattern. For example, it is possible to list all Python files in a folder as follows:

# List all files
for file in glob.glob('*.py'):
print(file)

The glob() function returns a list of files that contains the .py extension. A for loop is used to iterate through the list and print each file. When the preceding code snippet is executed, the output contains the list of all code samples belonging to this chapter (output truncated for representation):

read_from_file.py
config_parser_read.py
append_to_file.py
read_line_from_file.py
config_parser_modify.py
python_utils.py
config_parser_write.py
csv_write.py

This module is especially helpful with listing files that have a specific pattern. For example: Let's consider a scenario where you would like to upload files that were created from different trials of an experiment. You are only interested in files that are of the following format: file1xx.txt where x stands for any digit between 0 and 9. Those files could be sorted and listed as follows:

# List all files of the format 1xx.txt
for file in glob.glob('txt_files/file1[0-9][0-9].txt'):
print(file)

In the preceding example, [0-9] means that the file name could contain any digit between 0 and 9. Since we are looking for files of the file1xx.txt format, the search pattern that is passed an argument to the glob() function is file1[0-9][0-9].txt.

When the preceding code snippet is executed, the output contains all text files of the specified format:

txt_files/file126.txt
txt_files/file125.txt
txt_files/file124.txt
txt_files/file123.txt
txt_files/file127.txt

We came across this article that explains the use of expressions for sorting files: http://www.linuxjournal.com/content/bash-extended-globbing. The same concept can be extended to searching for files using the glob module.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.40.207