File

Collection of DIRAC useful file related modules.

Warning

By default on Error they return None.

DIRAC.Core.Utilities.File.checkGuid(guid)

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.

Warning

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.

Example

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

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

Parameters:
  • 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’)

Returns:

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

DIRAC.Core.Utilities.File.getGlobbedFiles(files)

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

DIRAC.Core.Utilities.File.getGlobbedTotalSize(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

DIRAC.Core.Utilities.File.getMD5ForFiles(fileList)

Calculate md5 for the content of all the files.

Parameters:

fileList (list) – list of paths

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

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.

Warning

On any exception it returns -1.

DIRAC.Core.Utilities.File.makeGuid(fileName=None)

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.

Warning

Could return None in case of OSError or IOError.

Parameters:

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)

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

Parameters:

filename (string) – name of file to be opened