os.writefile(myOutput, content) > set encoding


#1

I use “writefile” to create a new file and write a string to it. But the encoding of the new file seems not be be controlled by settings in Preference > Internationalization > Language-specific Default Encoding.

os.writefile(myOutput, content);

Is there any way to force UTF-8 encoding on the newly created file (myOutput)?

Thanks!


#2

I find that the problem is not with writefile.

My code is like this

var content = ko.views.manager.currentView.scimoz.text;

var myOutput = "C:\\000\\aaa3.txt";

var os = Components.classes["@activestate.com/koOs;1"].getService(Components.interfaces.koIOs);

os.writefile(myOutput, content); 

If content is “aaa”, the output file is a good file with UTF-8.

If content is “aaöaaa”, the output file is corrupt.

Hope to have your suggestion! Thank you!


#3

For C#, they have something like this:

File.WriteAllText(filepath, filetext, Encoding.Unicode);


#4

I’m not able to able to reproduce an issue using koOS but you could try using the JS SDK instead:

var content = ko.views.manager.currentView.scimoz.text;
var myOutputFile = require("ko/file").open("C:\\000\\aaa3.txt", "w")
myOutputFile.write(content)
myOutputFile.close()

FYI, the service you used is using Python to open(path, 'wb').write(content). Komodo preferences have no effect on using this method to write to a file.

  • Carey

#5

@careyh Thank you for your solution!

I tried it in a JS script, but got error “ReferenceError: require is not defined”.

var content = ko.views.manager.currentView.scimoz.text;

var myOutput = "C:\\000\\111\\" + ko.views.manager.currentView.koDoc.baseName;

var myOutputFile = require("ko/file").open(myOutput, "w")

myOutputFile.write(content)

myOutputFile.close()

It seems that something like “include” in C is needed to get the JS SDK activated. Could you kindly give more clues? Thanks!


#6

@surfactant, what version of Komodo are you running?

  • Carey

#7

Komodo Edit v.7.1.3

I had accumulated a lot of scripts during last years. I’m afraid it would be a formidable project to adapt them to newer versions of Komodo. That’s why I still work with v.7.

Thanks!


#8

@surfactant have you tried migrating yet? We haven’t phased out any legacy libraries so all your code SHOULD work. If you haven’t tried it might be work a shot. Komodo 11 is free by just creating an account on the new Active State platform right now:
https://platform.activestate.com/

If you’re not into upgrading, can you clarify how you’re creating the file?

  • Carey

#9

@careyh About 3 years ago, I tried a newer version, but ran into some problems. After that I went back to v.7.

I don’t know what you mean by “creating the file”. Very often I use Komodo to manipulate web pages by RegEx batchwise, but not as a programming tool. I must say that Komodo is really great for the job.


#10

I’m clarifying this comment. You don’t create the file manually, correct? Running writefile results in a new file? I’m not able to reproduce the issue so I want to try and figure out what we’re missing from your workflow that might be causing the issue. So far, from what i see, your use case should work fine. using writefile to write “aaöaaa” to a file shouldn’t corrupt the file.

  • carey

#11

@careyh

You can reproduce it by doing:

  1. create test.txt, paste aaöaaa in it and save it with UTF-8.

  2. open test.txt in Komodo

  3. run the script, and aaa3.txt is generated.

    var content = ko.views.manager.currentView.scimoz.text;
    var myOutput = “C:\\000\\aaa3.txt”;
    var os = Components.classes["@activestate.com/koOs;1"].getService(Components.interfaces.koIOs);
    os.writefile(myOutput, content);

  4. simply click aaa3.txt to open it, you see: aa?aaa

Thank you!


#12

I’m still not able to reproduce the issue. In step 1 are you using Komodo to create test.txt or something else? I used Komodo.

Everything works fine for me. We’re missing something.
This is the output from my JS console session in Komodo.

var path = "C:\\Users\\cgcho\\Projects\\TEMP\\aaa3.txt"
var view = require("ko/views").current().get()
var content = view.scimoz.text
content
String
"aaöaaa"
var os = Components.classes["@activestate.com/koOs;1"].getService(Components.interfaces.koIOs)
os.writefile(path, content)

The aaa3.txt file is updated appropriately.


#13

Hi @careyh Thank you for your reply!

In step 1, test.txt is simply created by windows notepad. It’s really odd that you can’t reproduce the issue.


#14

The only thing I can think of to try now is to attempt to reproduce the issue in Safe Mode; Help > Troubleshooting > Restart in Safe-mode.

  • Carey