A dataset of all files matching one or more glob patterns. NOTE: The default behavior of this method is to return filenames in a non-deterministic random shuffled order. Pass a `seed` or `shuffle=False` to get results in a deterministic order. Example: If we had the following
(file_pattern, shuffle=None, seed=None)
| 840 | |
| 841 | @staticmethod |
| 842 | def list_files(file_pattern, shuffle=None, seed=None): |
| 843 | """A dataset of all files matching one or more glob patterns. |
| 844 | |
| 845 | NOTE: The default behavior of this method is to return filenames in |
| 846 | a non-deterministic random shuffled order. Pass a `seed` or `shuffle=False` |
| 847 | to get results in a deterministic order. |
| 848 | |
| 849 | Example: |
| 850 | If we had the following files on our filesystem: |
| 851 | - /path/to/dir/a.txt |
| 852 | - /path/to/dir/b.py |
| 853 | - /path/to/dir/c.py |
| 854 | If we pass "/path/to/dir/*.py" as the directory, the dataset |
| 855 | would produce: |
| 856 | - /path/to/dir/b.py |
| 857 | - /path/to/dir/c.py |
| 858 | |
| 859 | Args: |
| 860 | file_pattern: A string, a list of strings, or a `tf.Tensor` of string type |
| 861 | (scalar or vector), representing the filename glob (i.e. shell wildcard) |
| 862 | pattern(s) that will be matched. |
| 863 | shuffle: (Optional.) If `True`, the file names will be shuffled randomly. |
| 864 | Defaults to `True`. |
| 865 | seed: (Optional.) A `tf.int64` scalar `tf.Tensor`, representing the random |
| 866 | seed that will be used to create the distribution. See |
| 867 | `tf.compat.v1.set_random_seed` for behavior. |
| 868 | |
| 869 | Returns: |
| 870 | Dataset: A `Dataset` of strings corresponding to file names. |
| 871 | """ |
| 872 | with ops.name_scope("list_files"): |
| 873 | if shuffle is None: |
| 874 | shuffle = True |
| 875 | file_pattern = ops.convert_to_tensor( |
| 876 | file_pattern, dtype=dtypes.string, name="file_pattern") |
| 877 | matching_files = gen_io_ops.matching_files(file_pattern) |
| 878 | |
| 879 | # Raise an exception if `file_pattern` does not match any files. |
| 880 | condition = math_ops.greater(array_ops.shape(matching_files)[0], 0, |
| 881 | name="match_not_empty") |
| 882 | |
| 883 | message = math_ops.add( |
| 884 | "No files matched pattern: ", |
| 885 | string_ops.reduce_join(file_pattern, separator=", "), name="message") |
| 886 | |
| 887 | assert_not_empty = control_flow_ops.Assert( |
| 888 | condition, [message], summarize=1, name="assert_not_empty") |
| 889 | with ops.control_dependencies([assert_not_empty]): |
| 890 | matching_files = array_ops.identity(matching_files) |
| 891 | |
| 892 | dataset = Dataset.from_tensor_slices(matching_files) |
| 893 | if shuffle: |
| 894 | # NOTE(mrry): The shuffle buffer size must be greater than zero, but the |
| 895 | # list of files might be empty. |
| 896 | buffer_size = math_ops.maximum( |
| 897 | array_ops.shape(matching_files, out_type=dtypes.int64)[0], 1) |
| 898 | dataset = dataset.shuffle(buffer_size, seed=seed) |
| 899 | return dataset |