udapi.core.files module

Files is a helper class for iterating over filenames.

class udapi.core.files.Files(filenames=None, filehandle=None, encoding='utf-8')[source]

Bases: object

Helper class for iterating over filenames.

It is used e.g. in udapi.core.basereader (as self.files = Files(filenames=pattern)). Constructor takes various arguments: >>> files = Files([‘file1.txt’, ‘file2.txt’]) # list of filenames or >>> files = Files(‘file1.txt,file2.txt’) # comma- or space-separated filenames in string >>> files = Files(‘file1.txt,file2.txt.gz’) # supports automatic decompression of gz, xz, bz2 >>> files = Files(@my.filelist !dir??/file*.txt’) # @ marks filelist, ! marks wildcard pattern The @filelist and !wildcard conventions are used in several other tools, e.g. 7z or javac.

Usage: >>> while (True): >>> filename = files.next_filename()

if filename is None:

break

or >>> filehandle = files.next_filehandle()

property filename

Property with the current file name.

has_next_file()[source]

Is there any other file in the queue after the current one?

next_filehandle()[source]

Go to the next file and retrun its filehandle or None (meaning no more files).

next_filename()[source]

Go to the next file and retrun its filename or None (meaning no more files).

property number_of_files

Propery with the total number of files.

string_to_filenames(string)[source]

Parse a pattern string (e.g. ‘!dir??/file*.txt’) and return a list of matching filenames.

If the string starts with ! it is interpreted as shell wildcard pattern. If it starts with @ it is interpreted as a filelist with one file per line. The string can contain more filenames (or ‘!’ and ‘@’ patterns) separated by spaces or commas. For specifying files with spaces or commas in filenames, you need to use wildcard patterns or ‘@’ filelist. (But preferably don’t use such filenames.)