Python file.read () view garbage characters at the beginning of a file

I am trying to use Python to merge several javascript files together before minimizing them, basically like this:

outfile = open("output.js", "w")
for somefile in a_list_of_file_names:
    js = open(somefile)
    outfile.write(js.read())
    js.close()
outfile.close()

minifier complains about invalid characters and syntax errors at the beginning of each file, so I did some diagnostics.

>>> r = open("output.js")
>>> somestring = r.readline()
>>> somestring
'\xef\xbb\xbfvar $j = jQuery.noConflict(),\n'
>>> print somestring
var $j = jQuery.noConflict(),

The first line of the file should, of course, be "var $ j = jQuery.noConflict (),"

In case it matters, I work from Windows.

Any thoughts?

Edit: this is what I get from minifier:

U:\>java -jar c:\path\yuicompressor-2.4.2.jar c:\path\somefile.js -o c:\path\bccsminified.js --type js -v

[INFO] Using charset Cp1252

[ERROR] 1:2:illegal character

[ERROR] 1:2:syntax error

[ERROR] 1:3:illegal character
+3
source share
2 answers

EF BB BF is a Unicode Byte Character (BOM). This is valid in your files. This is why Python sees this.

/ , , .

+4

UTF-8 ( ). , .

+5

Source: https://habr.com/ru/post/1759353/


All Articles