Debugging using File Operations
The following are some file operations which are useful for debugging. The following examples can be found in the FileOperationForDebugging.vbs and FileOperationForDebugging.py files located in the <Installdir>\CodeSamplesPlus folder.
VBScript sample
' Creates a specified file and returns a TextStream object that can be used to read from or write to the file.
' Example of usage:
' Set f = CreateFile("d:\temp\beenhere.txt", True)
' f.WriteLine Now
' f.Close
Function CreateFile(sFilename, bOverwrite)
Set fso = CreateObject("Scripting.FileSystemObject")
Set CreateFile = fso.CreateTextFile(sFilename, bOverwrite)
End Function
' Opens a specified file and returns a TextStream object that can be used to read from, write to, or append to the file.
' iomode: 1 - ForReading, 2 - ForWriting, 8 - ForAppending
' Example of usage
' Set f = OpenFile("d:\temp\beenhere.txt", 2, True)
' f.WriteLine Now
' f.Close
Function OpenFile(sFilename, iomode, create)
Set fso = CreateObject("Scripting.FileSystemObject")
Set OpenFile = fso.OpenTextFile(sFilename, iomode, create)
End Function
' Appends a line to a file.
' Example of usage:
' AppendToFile "d:\temp\beenhere.txt", Now
Function AppendToFile(sFilename, sLine)
Const ForAppending = 8
If sFilename = "" Then
sFilename = Environment("SystemTempDir") & "\QTDebug.txt"
End If
Set f = OpenFile(sFilename, ForAppending, True)
f.WriteLine sLine
f.Close
End Function
' Writes a line to a file.
' Destroys the current content of the file.
' Example of usage:
' WriteToFile "d:\temp\beenhere.txt", Now
Function WriteToFile(sFilename, sLine)
Const ForWriting = 2
If sFilename = "" Then
sFilename = Environment("SystemTempDir") & "\QTDebug.txt"
End If
Set f = OpenFile(sFilename, ForWriting, True)
f.WriteLine sLine
f.Close
End FunctionPython sample
import os
import tempfile
from datetime import datetime
# Creates a specified file and returns a stream that can be used to read from or write to the file.
def create_file(filename, overwrite=True):
mode = "w" if overwrite else "x"
return open(filename, mode, encoding="utf-8")
# Opens a specified file and returns a stream that can be used to read from, write to, or append to the file.
# iomode: 1 - reading, 2 - writing, 8 - appending
def open_file(filename, iomode, create=True):
mode_map = {1: "r", 2: "w", 8: "a"}
if iomode not in mode_map:
raise ValueError("Invalid IO mode")
if create and iomode in (2, 8):
open(filename, "a", encoding="utf-8").close()
return open(filename, mode_map[iomode], encoding="utf-8")
# Appends a line to a file.
def append_to_file(filename, line):
if not filename:
filename = os.path.join(tempfile.gettempdir(), "QTDebug.txt")
with open(filename, "a", encoding="utf-8") as f:
f.write(f"{line}\\n")
# Writes a line to a file (overwriting current content).
def write_to_file(filename, line):
if not filename:
filename = os.path.join(tempfile.gettempdir(), "QTDebug.txt")
with open(filename, "w", encoding="utf-8") as f:
f.write(f"{line}\\n")
# Example usage
f = create_file(r"c:\temp\beenhere.txt", overwrite=True)
f.write("Been here\\n")
f.close()
f = open_file(r"d:\temp\beenhere.txt", 2, True)
f.write("Hello\\n")
f.close()
append_to_file(r"d:\temp\beenhere.txt", datetime.now())
write_to_file(r"d:\temp\beenhere.txt", datetime.now())

