5. CFile module¶
- class PythonExtensionsCollection.File.CFile.CFile(sFile=None)[source]¶
Bases:
objectThe class
CFileprovides 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
bSuccessandsResult.bSuccessisTruein case of no error occurred.bSuccessisFalsein case of an error occurred.bSuccessisNonein case of a very fatal error occurred (exceptions).sResultcontains 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
CopyToorMoveToto 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
Contentto file.If
Contentis not a string, theWritemethod resolves the data structure (thereforeContentcan also be of typelist,tuple,set,dict,dotdict).Adds vertical space
nVSpaceAfter(= number of blank lines) afterContent.Prints
Contentalso to screen in case ofbToScreenisTrue(default:False).Returns
bSuccessandsResult(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
bOverwriteisTrue, than the destination file will be overwritten.In case of the destination file already exists and
bOverwriteisFalse(default), than the destination file will not be overwritten andCopyToreturnsbSuccess = False.Returns
bSuccessandsResult(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
bSuccessandsResult(feedback).
- GetFileInfo()[source]¶
Returns the following informations about the file (encapsulated within a dictionary):
Key sFile
Path and name of current file
Key bFileIsExisting
Trueif 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
Trueif 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
bOverwriteisTrue, than the destination file will be overwritten.In case of the destination file already exists and
bOverwriteisFalse(default), than the destination file will not be overwritten andCopyToreturnsbSuccess = False.Returns
bSuccessandsResult(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
bSuccessandsResult(feedback).The method takes care of opening and closing the file. The complete file content is read by
ReadLinesin 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
Contentto file.If
Contentis not a string, theWritemethod resolves the data structure (thereforeContentcan also be of typelist,tuple,set,dict,dotdict).Adds vertical space
nVSpaceAfter(= number of blank lines) afterContent.Prints
Contentalso to screen in case ofbToScreenisTrue(default:False).Returns
bSuccessandsResult(feedback).