CkFileAccess Python Programming
Reference Documentation

CkFileAccess

API for reading and writing files, creating and deleting directories, deleting directory trees, splitting and re-joining large files, etc. This is a freeware class/library. The reason for its existence is that in some programming languages, file I/O API's are limited or difficult to understand/use. This API provides an identical FILE I/O API across all programming languages supported by Chilkat.

Object Creation

obj = chilkat.CkFileAccess()

Properties

# str is a CkString object (output)
get_CurrentDir( str )

The current working directory of the calling process.

# Returns a boolean value
get_EndOfFile( )

Returns True if the current open file is at the end-of-file.

# str is a CkString object (output)
LastErrorHtml( str )

Error information in HTML format for the last method called.

# str is a CkString object (output)
LastErrorText( str )

Error information in plain-text format for the last method called.

# str is a CkString object (output)
LastErrorXml( str )

Error information in XML format for the last method called.

# Returns a boolean value
get_Utf8( )

# b is a boolean (input)
put_Utf8( b )

To be documented soon...

Methods

# text is a string (input)
# Returns a boolean value
AppendAnsi( text )

Appends a string using the ANSI character encoding to the currently open file.

Returns True for success, False for failure.

# str is a string (input)
# charset is a string (input)
# Returns a boolean value
AppendText( str, charset )

Appends a string using the character encoding specified by str to the currently open file.

Returns True for success, False for failure.

# Returns a boolean value
AppendUnicodeBOM( )

Appends the 2-byte Unicode BOM (little endian) to the currently open file.

Returns True for success, False for failure.

# Returns a boolean value
AppendUtf8BOM( )

Appends the 3-byte utf-8 BOM to the currently open file.

Returns True for success, False for failure.

# dirPath is a string (input)
# Returns a boolean value
DirAutoCreate( dirPath )

Creates all directories necessary such that the entire dirPath exists.

Returns True for success, False for failure.

# dirPath is a string (input)
# Returns a boolean value
DirCreate( dirPath )

Creates a new directory specified by dirPath.

Returns True for success, False for failure.

# dirPath is a string (input)
# Returns a boolean value
DirDelete( dirPath )

Deletes the directory specified by dirPath.

Returns True for success, False for failure.

# filePath is a string (input)
# Returns a boolean value
DirEnsureExists( filePath )

Same as DirAutoCreate, except the argument is a file path (the last part of the path is a filename and not a directory). Creates all missing directories such that filePath may be created.

Returns True for success, False for failure.

FileClose( )

Closes the currently open file.

# existingFilepath is a string (input)
# newFilepath is a string (input)
# failIfExists is a boolean (input)
# Returns a boolean value
FileCopy( existingFilepath, newFilepath, failIfExists )

Copys existingFilepath to newFilepath. If failIfExists is True and newFilepath already exists, then an error is returned.

Returns True for success, False for failure.

# filePath is a string (input)
# Returns a boolean value
FileDelete( filePath )

Deletes the file specified by filePath.

Returns True for success, False for failure.

# filePath is a string (input)
# Returns a boolean value
FileExists( filePath )

Returns True if filePath exists, otherwise returns False.

# filePath is a string (input)
# accessMode is an integer (input)
# shareMode is an integer (input)
# createDisposition is an integer (input)
# attributes is an integer (input)
# Returns a boolean value
FileOpen( filePath, accessMode, shareMode, createDisposition, attributes )

Opens a file for reading or writing. The arguments mirror the Windows CreateFile function:

Access Modes:
GENERIC_READ	(0x80000000)
GENERIC_WRITE (0x40000000)

Share Modes:
FILE_SHARE_READ(0x00000001)
FILE_SHARE_WRITE(0x00000002)

Create Dispositions
CREATE_NEW          1
CREATE_ALWAYS       2
OPEN_EXISTING       3
OPEN_ALWAYS         4
TRUNCATE_EXISTING   5

// Attributes:
FILE_ATTRIBUTE_READONLY         0x00000001
FILE_ATTRIBUTE_HIDDEN           0x00000002
FILE_ATTRIBUTE_SYSTEM           0x00000004
FILE_ATTRIBUTE_DIRECTORY        0x00000010
FILE_ATTRIBUTE_ARCHIVE          0x00000020
FILE_ATTRIBUTE_NORMAL           0x00000080
FILE_ATTRIBUTE_TEMPORARY	   0x00000100

Returns True for success, False for failure.

# maxNumBytes is an integer (input)
# outBytes is a CkByteData object (output)
# Returns a boolean value
FileRead( maxNumBytes, outBytes )

Reads bytes from the currently open file. maxNumBytes specifies the maximum number of bytes to read. Returns an empty byte array on error.

# existingFilepath is a string (input)
# newFilepath is a string (input)
# Returns a boolean value
FileRename( existingFilepath, newFilepath )

Renames a file from existingFilepath to newFilepath.

Returns True for success, False for failure.

# offset is an integer (input)
# origin is an integer (input)
# Returns a boolean value
FileSeek( offset, origin )

Sets the file pointer for the currently open file. The offset is an offset in bytes from the origin. The origin can be one of the following:

0 = Offset is from beginning of file.
1 = Offset is from current position of file pointer.
2 = Offset is from the end-of-file (offset may be negative).

Returns True for success, False for failure.

# filePath is a string (input)
# Returns an integer value
FileSize( filePath )

Returns the size, in bytes, of a file. Returns -1 for failure.

# data is a CkByteData object (output)
# Returns a boolean value
FileWrite( data )

Writes bytes to the currently open file.

Returns True for success, False for failure.

# dirName is a string (input)
# prefix is a string (input)
# outStr is a CkString object (output)
# Returns a boolean value
GetTempFilename( dirName, prefix, outStr )

To be documented soon.

Returns True for success, False for failure.

# filePath is a string (input)
# encoding is a string (input)
# outStr is a CkString object (output)
# Returns a boolean value
ReadBinaryToEncoded( filePath, encoding, outStr )

Reads the entire contents of a binary file and returns it as an encoded string (using an encoding such as Base64, Hex, etc.) The encoding may be one of the following strings: base64, hex, qp, or url.

Returns True for success, False for failure.

# filePath is a string (input)
# outBytes is a CkByteData object (output)
# Returns a boolean value
ReadEntireFile( filePath, outBytes )

Reads the entire contents of a binary file and returns the data.

# filePath is a string (input)
# charset is a string (input)
# strFileContents is a CkString object (output)
# Returns a boolean value
ReadEntireTextFile( filePath, charset, strFileContents )

Reads the entire contents of a text file, interprets the bytes according to the character encoding specified by charset, and returns the text file as a string.

Returns True for success, False for failure.

# partsDirPath is a string (input)
# partPrefix is a string (input)
# partExtension is a string (input)
# reassembledFilename is a string (input)
# Returns a boolean value
ReassembleFile( partsDirPath, partPrefix, partExtension, reassembledFilename )

Reassembles a file previously split by the SplitFile method.

Example: Reassemble a Previously Split File

# filename is a string (input)
# Returns a boolean value
SaveLastError( filename )

Saves the last error information to an XML formatted file.

# dirPath is a string (input)
# Returns a boolean value
SetCurrentDir( dirPath )

Sets the current working directory for the calling process to dirPath.

# fileToSplit is a string (input)
# partPrefix is a string (input)
# partExtension is a string (input)
# partSize is an integer (input)
# outputDirPath is a string (input)
# Returns a boolean value
SplitFile( fileToSplit, partPrefix, partExtension, partSize, outputDirPath )

Splits a file into chunks. Please refer to the example below:

Example: Split File into Chunks

# path is a string (input)
# Returns a boolean value
TreeDelete( path )

Deletes an entire directory tree (all files and sub-directories).

Returns True for success, False for failure.

# filePath is a string (input)
# fileData is a CkByteData object (output)
# Returns a boolean value
WriteEntireFile( filePath, fileData )

Opens/creates filePath, writes fileData, and closes the file.

Returns True for success, False for failure.

# filePath is a string (input)
# textData is a string (input)
# charset is a string (input)
# includedPreamble is a boolean (input)
# Returns a boolean value
WriteEntireTextFile( filePath, textData, charset, includedPreamble )

Opens filePath, writes textData using the character encoding specified by charset, and closes the file. If includedPreamble is True and the charset is Unicode or utf-8, then the BOM is included at the beginning of the file.

Returns True for success, False for failure.

# Returns a string
currentDir( )

The current working directory of the calling process.

# dirName is a string (input)
# prefix is a string (input)
# Returns a string
getTempFilename( dirName, prefix )

To be documented soon.

# Returns a string
lastErrorHtml( )

Error information in HTML format for the last method called.

# Returns a string
lastErrorText( )

Error information in plain-text format for the last method called.

# Returns a string
lastErrorXml( )

Error information in XML format for the last method called.

# filePath is a string (input)
# encoding is a string (input)
# Returns a string
readBinaryToEncoded( filePath, encoding )

Reads the entire contents of a binary file and returns it as an encoded string (using an encoding such as Base64, Hex, etc.) The encoding may be one of the following strings: base64, hex, qp, or url.

Returns True for success, False for failure.

# filePath is a string (input)
# charset is a string (input)
# Returns a string
readEntireTextFile( filePath, charset )

Reads the entire contents of a text file, interprets the bytes according to the character encoding specified by charset, and returns the text file as a string.

Returns True for success, False for failure.