Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program throws (Non-UTF-8 code starting with '\xff' in file) when running from command prompt

Tags:

python

Below is the code:

def add(a,b):
    return a + b
while True:
    try:
        a=float(input('Please enter a number'))
        break
    except:
        print ('Invalid Number, please re-enter')
while True:
    try:
        b=float(input('Please enter a number'))
        break
    except:
        print ('Invalid Number, please re-enter')
print(add(a,b))
like image 505
Sammy123 Avatar asked Jan 28 '23 06:01

Sammy123


2 Answers

Scenario: I created a Python file via PowerShell and edited it in VS Code. And, I got a similar error.
Problem: Apparently Powershell and VS Code use conflicting encoding schemas. For details see: Understanding file encoding in VS Code and PowerShell
My Solution: Recreate file in VS Code.

like image 154
gurpartap Avatar answered Jan 29 '23 18:01

gurpartap


I think the issue may be how you are calling it form the command line. You should change directories to where your python interpreter is and then point it to the script

c:\users\test>cd c:\python2.7
c:\python2.7>python.exe "c:\users\test\desktop\test_script.py"

Just use the path to your python exe and then replace my desktop with the path to your script.

like image 33
Dillon_Su Avatar answered Jan 29 '23 19:01

Dillon_Su