Changda Li Changda Li - 11 months ago 40
Linux Question

python code not working after copied from windows to linux

I just wrote a small program in python, which is:

#!/usr/bin/env python
print "hello"

It works in windows. And when I type this code in linux, it works, too.

But when I copy the python file from windows to linux in my VBox, this code doesn't work, and an error appears which is:

: no such file or directory
Why does that happen? And what should I do to fix it?

Answer Source

Perhaps you get an error because of different line endings on windows and linux? Windows uses "\r\n" and Linux just "\n".

You could write script that would get rid of "\r" on linux, for example:

EDIT: I have realized that carriage returns are seen only in binary mode. So script should do something like this

with open('', 'rb') as file:
    data =
    data = data.replace(b'\r\n', b'\n')
with open('', 'wb') as file: