PYTHON-2.x Syntax error on line 1, but I can’t see it?

this directory contains the following file: /Users/whiteglider/Documents

file name: server.py

this is my practice code that I just copied from http://www.tutorialspoint.com/python/python_networking.htm

  import socket s=socket.socket() host=socket.gethostname() port=12345 s.bind((host,port)) s.listen(5) while True: c, addr = s.accept() print 'Got connection from', addr c.send ('Thank you for connecting') c.close() 

when i run it in terminal i print

 $ python /Users/whiteglider/Documents/server.py 

then I get:

 File "/Users/whiteglider/Documents/server.py", line 1 {\rtf1\ansi\ansicpg1252\cocoartf949\cocoasubrtf540 ^ SyntaxError: unexpected character after line continuation character 

even if I change the directory where the server.py file is located and it starts

 python server.py 

I still get the same result.

(mac leopard 10.5.8)

+4
source share
2 answers

You saved the file as a Rich Text Format file, not a regular text file.

I don’t know which editor you are using, but be sure to save the file as plain text / ASCII text, something like this, not RTF.

+8
source

A line continuation character is usually a backslash at the end of a line. However, I do not see in your example.

Can you run a simple hello world application, for example the following:

 print 'hello world' 

(save the above in the hello.py file and run)

Is python different from an interactive prompt?

Perhaps this is due to what type of lines your file has? '\ n' or '\ r \ n'? I don’t think it should matter, but who knows ...

0
source

All Articles