Collection of DIRAC useful file related modules.


By default on Error they return None.


Checks whether a supplied GUID is of the correct format. The guid is a string of 36 characters [0-9A-F] long split into 5 parts of length 8-4-4-4-12.


As we are using GUID produced by various services and some of them could not follow convention, this function is passing by a guid which can be made of lower case chars or even just have 5 parts of proper length with whatever chars.


guid (string) – string to be checked


True (False) if supplied string is (not) a valid GUID.

DIRAC.Core.Utilities.File.convertSizeUnits(size, srcUnit, dstUnit)

Converts a number from a given source unit to a destination unit.


In [1]: convertSizeUnits(1024, ‘B’, ‘kB’) Out[1]: 1

In [2]: convertSizeUnits(1024, ‘MB’, ‘kB’) Out[2]: 1048576

  • size – number to convert

  • srcUnit – unit of the number. Any of ( ‘B’, ‘kB’, ‘MB’, ‘GB’, ‘TB’, ‘PB’)

  • dstUnit – unit expected for the return. Any of ( ‘B’, ‘kB’, ‘MB’, ‘GB’, ‘TB’, ‘PB’)


the size number converted in the dstUnit. In case of problem -sys.maxint is returned (negative)

DIRAC.Core.Utilities.File.generateGuid(checksum, checksumtype)

Generate a GUID based on the file checksum


Get list of files or a single file. Globs the parameter to allow regular expressions.

Params list files:

list or tuple of strings of files


Get total size of a list of files or a single file. Globs the parameter to allow regular expressions.

Params list files:

list or tuple of strings of files


Calculate md5 for the content of all the files.


fileList (list) – list of paths

DIRAC.Core.Utilities.File.getSize(fileName: PathLike) int

Get size of a file.


fileName (string) – name of file to be checked

The os module claims only OSError can be thrown, but just for curiosity it’s catching all possible exceptions.


On any exception it returns -1.


Utility to create GUID. If a filename is provided the GUID will correspond to its content’s hexadecimal md5 checksum. Otherwise a random seed is used to create the GUID. The format is capitalized 8-4-4-4-12.


Could return None in case of OSError or IOError.


fileName (string) – name of file

DIRAC.Core.Utilities.File.mkDir(path, mode=None)

Emulate ‘mkdir -p path’ (if path exists already, don’t raise an exception)

  • path (str) – directory hierarchy to create

  • mode (int) – Use this mode as the mode for new directories, use python default if None.

Protected creation of symbolic link

DIRAC.Core.Utilities.File.secureOpenForWrite(filename=None, *, text=True)

Securely open a file for writing.

If filename is not provided, a file is created in tempfile.gettempdir(). The file always created with mode 600.


filename (string) – name of file to be opened