5. CFile module

class PythonExtensionsCollection.File.CFile.CFile(sFile=None)[source]

Bases: object


The class CFile provides a small set of file functions with extended parametrization (like switches defining if a file is allowed to be overwritten or not).

Most of the functions at least returns bSuccess and sResult.

  • bSuccess is True in case of no error occurred.

  • bSuccess is False in case of an error occurred.

  • bSuccess is None in case of a very fatal error occurred (exceptions).

  • sResult contains details about what happens during computation.

Every instance of CFile handles one single file only and forces exclusive access to this file.

It is not possible to create an instance of this class with a file that is already in use by another instance.

It is also not possible to use CopyTo or MoveTo to overwrite files that are already in use by another instance. This makes the file handling more save against access violations.


Append(Content='', nVSpaceAfter=0, sPrefix=None, bToScreen=False)[source]

Appends the content of a variable Content to file.

If Content is not a string, the Write method resolves the data structure (therefore Content can also be of type list, tuple, set, dict, dotdict).

Adds vertical space nVSpaceAfter (= number of blank lines) after Content.

Prints Content also to screen in case of bToScreen is True (default: False).

Returns bSuccess and sResult (feedback).


Close()[source]

Closes the opened file.

Returns bSuccess and sResult (feedback).


CopyTo(sDestination=None, bOverwrite=False)[source]

Copies the current file to sDestination, that can either be a path without file name or a path together with a file name.

In case of the destination file already exists and bOverwrite is True, than the destination file will be overwritten.

In case of the destination file already exists and bOverwrite is False (default), than the destination file will not be overwritten and CopyTo returns bSuccess = False.

Returns bSuccess and sResult (feedback).


Delete(bConfirmDelete=True)[source]

Deletes the current file.

bConfirmDelete

Defines if it will be handled as error if the file does not exist.

If True: If the file does not exist, the method indicates an error (bSuccess = False).

If False: It doesn’t matter if the file exists or not.

Returns bSuccess and sResult (feedback).


GetFileInfo()[source]

Returns the following informations about the file (encapsulated within a dictionary):

Key sFile

Path and name of current file

Key bFileIsExisting

True if file is existing, otherwise not

Key sFileName

The name of the current file (incl. extension)

Key sFileExtension

The extension of the current file

Key sFileNameOnly

The pure name of the current file (without extension)

Key sFilePath

The the path to current file

Key bFilePathIsExisting

True if file path is existing, otherwise not


MoveTo(sDestination=None, bOverwrite=False)[source]

Moves the current file to sDestination, that can either be a path without file name or a path together with a file name.

In case of the destination file already exists and bOverwrite is True, than the destination file will be overwritten.

In case of the destination file already exists and bOverwrite is False (default), than the destination file will not be overwritten and CopyTo returns bSuccess = False.

Returns bSuccess and sResult (feedback).


ReadLines(bCaseSensitive=True, bSkipBlankLines=False, sComment=None, sStartsWith=None, sEndsWith=None, sStartsNotWith=None, sEndsNotWith=None, sContains=None, sContainsNot=None, sInclRegEx=None, sExclRegEx=None, bLStrip=False, bRStrip=True, bToScreen=False)[source]

Reads content from current file. Returns an array of lines together with bSuccess and sResult (feedback).

The method takes care of opening and closing the file. The complete file content is read by ReadLines in one step, but with the help of further parameters it is possible to reduce the content by including and excluding lines.

T.B.C.


Write(Content='', nVSpaceAfter=0, sPrefix=None, bToScreen=False)[source]

Writes the content of a variable Content to file.

If Content is not a string, the Write method resolves the data structure (therefore Content can also be of type list, tuple, set, dict, dotdict).

Adds vertical space nVSpaceAfter (= number of blank lines) after Content.

Prints Content also to screen in case of bToScreen is True (default: False).

Returns bSuccess and sResult (feedback).


class PythonExtensionsCollection.File.CFile.enFileStatiType[source]

Bases: object

closed = 'closed'
openedforappending = 'openedforappending'
openedforreading = 'openedforreading'
openedforwriting = 'openedforwriting'