3

I am running a simple: echo print "Hello!!" > prog.py on PowerShell (PS). It was all going well untill I set .py programs to run by default with IDLE.bat. As soon as I did that, when I type .\prog.py, PowerShell runs Idle, so I had the "brilliant idea" to set .py to run with PS. I set Idle again as default program for .py, but now I am not able to create my .py files from PS, and I notice that it inserts characters, a carriage return between print and Hello, and spaces between all letters. Like this:

ÿþp r i n t

H e l l o

Running python prog.py I get a SyntaxError, like this:

PS C:\> python prog.py
  File "prog.py", line 1
SyntaxError: Non-ASCII character '\xff' in file prog.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Does PS lose the encoding? How can I solve this?

I am using Win10(PSv5.0); Python 2.7.11

1
  • I can't really answer your question but why do you want to create your programs in that way? Can't you just write your scripts.. normally? Commented Jun 4, 2016 at 20:54

1 Answer 1

1

ÿþ is a byte order mark. You're looking at Windows Unicode, which is UCS-2 LE or UTF-16, which is the common encoding format that the .Net Framework uses. The "spaces" are happening because Python isn't interpreting the file as being encoded with multibyte characters, which is what it is.

Try specifying the encoding explicitly:

Write-Output 'print "Hello World!!"' | Out-File -FilePath .\prog.py -Encoding ascii
Sign up to request clarification or add additional context in comments.

3 Comments

-Path wasn't available for me on PowerShell 5.0 but I found this works for me in Python 3.5, Write-Output 'print ("Hello!!")' | Out-File -FilePath .\prog.py -Encoding ascii Not sure about the parenthesis being needed in Python 2.7 in t he case of the OP's situation.
@BoogaRoo Oh, sorry. It's -FilePath, not -Path. Darn inconsistent parameter names!
No worries. Your answer piqued my interest so much I went and installed Python!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.