Trying to tweak Python code using the below VBA code - but getting weird results

Viewed 49

I am trying to find and replace some statements in an existing Python script via VBA through the below code:

Private Sub FindReplaceStuff()

Dim fso As Scripting.FileSystemObject, Var As String
Set fso = New FileSystemObject

Dim path As String
path = Application.ActiveWorkbook.path

Dim orig_file As Scripting.TextStream
Set orig_file = fso.OpenTextFile(path & "\" & "test2.py")

Dim new_file As Scripting.TextStream
Set new_file = fso.CreateTextFile(path & "\" & "test3.py")

Do While Not orig_file.AtEndOfStream
    Var = orig_file.ReadLine
    newline = replace(Var, "debug.INFO", "print")
    new_file.WriteLine newline
Loop

orig_file.Close
new_file.Close
   

End Sub

It is indeed creating a new file, but in that file, I see characters like these which are quite out of place- ∀∀∀䄀搀搀 䘀甀渀挀琀椀漀渀∀∀∀. The new file has a structure similar to the previous file, but it mostly has these characters which I think are Chinese characters, while I have no reference to the language anywhere on my computer.

I am not sure what went wrong here. Could you please advise?

Thanks a lot for your input! Hoping for your kind help.

Aadya

0 Answers
Related