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.

Parameters:guid (string) – string to be checked
Returns: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 the common path for all files in the file list.

Parameters:files (list) – list of strings with paths

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.

Parameters:fileList (list) – list of paths

Get size of a file.

Parameters: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.

Parameters:fileName (string) – name of file

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

Protected creation of simbolic link