Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running .py in PowerShell [duplicate]

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

like image 400
NewSQL Avatar asked Mar 28 '26 17:03

NewSQL


1 Answers

ÿþ 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
like image 161
Bacon Bits Avatar answered Mar 31 '26 05:03

Bacon Bits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!