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
andsResult
.bSuccess
isTrue
in case of no error occurred.bSuccess
isFalse
in case of an error occurred.bSuccess
isNone
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
orMoveTo
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, theWrite
method resolves the data structure (thereforeContent
can also be of typelist
,tuple
,set
,dict
,dotdict
).Adds vertical space
nVSpaceAfter
(= number of blank lines) afterContent
.Prints
Content
also to screen in case ofbToScreen
isTrue
(default:False
).Returns
bSuccess
andsResult
(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
isTrue
, than the destination file will be overwritten.In case of the destination file already exists and
bOverwrite
isFalse
(default), than the destination file will not be overwritten andCopyTo
returnsbSuccess = False
.Returns
bSuccess
andsResult
(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
andsResult
(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 notKey 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
isTrue
, than the destination file will be overwritten.In case of the destination file already exists and
bOverwrite
isFalse
(default), than the destination file will not be overwritten andCopyTo
returnsbSuccess = False
.Returns
bSuccess
andsResult
(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
andsResult
(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, theWrite
method resolves the data structure (thereforeContent
can also be of typelist
,tuple
,set
,dict
,dotdict
).Adds vertical space
nVSpaceAfter
(= number of blank lines) afterContent
.Prints
Content
also to screen in case ofbToScreen
isTrue
(default:False
).Returns
bSuccess
andsResult
(feedback).