Question

I'm using this python script to add a copyright spiel to the start of all my C# scripts

import re
import shutil
import os

copyrightloc =  'C:/DATA/pyscripts/copyright.txt'
rootdir = 'C:/DATA/pyscripts/02_CODE'
dstdir = 'C:/DATA/pyscripts/codecopy'

spielfile =  open(copyrightloc, "r") 
spiel = spielfile.read() 

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        if file.endswith(".cs"):
            with open(subdir+'/'+file, "r+") as codefile ,  open(dstdir+'/'+file, 'w') as destfile:
                destfile.write(spiel+'\n' + codefile.read())

As you can see I am adding the original string to the copyright string and writing it to a new file.

The files look fine when theyre finished but in every file, at the first line of the original file, I get a parsing error. For example, below shows an exerpt from the new file at the end of the copyright speil and the beginning of the original file...

          BLAH BLAH BLAH COPYRIGHT
  * OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  * CONTRACT, NEGLIGENCE, TORT OR OTHERWISE, ARISING OUT OF OR IN
  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE OR ITS DERIVATIVES.
  */


using UnityEngine;   [!!!ERROR IS SHOWN ON THIS LINE!!!]
using System.Collections;

public class Floop : MonoBehaviour {

    public rot glorb;
    public GameObject foo;

              BLAH BLAH BLAH MY CODE

I'm guessing there is some invisible character there like "end of file" or something but I cannot see anything in notepad++ when I select "show all characters" ... If I go to the start of the line in question and hit delete the error goes away.. How can I make my python script avoid this problem?

Was it helpful?

Solution 3

There is chance that in your copyright text there is unicode characters not properly encoded try use the codecs module

import re
import shutil
import os
import codecs

copyrightloc =  'C:/DATA/pyscripts/copyright.txt'
rootdir = 'C:/DATA/pyscripts/02_CODE'
dstdir = 'C:/DATA/pyscripts/codecopy'

spielfile =  codecs.open(copyrightloc, "r", encoding="utf8") 
spiel = spielfile.read() 

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        if file.endswith(".cs"):
            with codecs.open(subdir+'/'+file, "r+",encoding="utf8") as codefile ,  open(dstdir+'/'+file, 'w') as destfile:
                destfile.write(spiel+'\n' + codefile.read())

OTHER TIPS

The MSDN C# style guide says you should not use blocks of asterisks around comments. Can you try prefixing each line of the copyright with //?

Alternatively, you may be able to use this format (note lack of asterisk at the beginning of each line):

/*
copyright here
*/

Maybe your files contains a 'Byte order mark', which are some special characters at the beginning of the file to indicate the encoding.

Check this with a HEX editor if you see some extra characters before the ones that you expect.

If this is the case, then you should use the 'utf-8-sig' encoding. I'm not a python export, but your code could then look like this

...
spielfile =  codecs.open(copyrightloc, "r", encoding="utf-8-sig")
...
with codecs.open(subdir+'/'+file, "r+", encoding="utf-8-sig") as codefile ,  open(dstdir+'/'+file, 'w', encoding="utf-8-sig") as destfile:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top