一、原因:在把windows下的一个python脚本挪到linux下的时候,文件格式保存错误,应该存为unix格式。详细解释
二、解决方法:vim xx.py输入:
:set fileformat=unix
然后赋予其权限:
chmod a+x xx.py
最后执行:
./xx.py
三、linux下执行.py文件两种方式:
1.直接python执行:
python3 xx.py
2.脚本执行:
注意:这种方式程序文件首行要加上#! /usr/bin/env python3
其作用是"指定由哪个解释器来执行脚本",很多人在系统中同时安装了 Python2 和 Python3, 但是 2 和 3 是不兼容的, 所以执行脚本时必须指定解释器.
./xx.py
四、举例子:
aa.py
#!/usr/bin/env python3
from threading import Thread
from time import sleep
import random
import os
class MyThreadSing(Thread):
def run(self):
for i in range(3):
print('Sing...%d...%d'%(i,os.getpid()))
sleep(random.random())
class MyThreadDancer(Thread):
def run(self):
for i in range(3):
print('Dancer...%d...%d'%(i,os.getpid()))
sleep(random.random())
if __name__=='__main__':
sing=MyThreadSing(name='T-Sing')
print(sing.name)
print(sing.daemon)
sing.start()
dancer = MyThreadDancer(name='T-Dancer',daemon=True)
print(dancer.name)
print(dancer.daemon)
dancer.start()
#sleep(0.1)
print('game over...')
方式一:
[root@localhost demo01]# python3 aa.py
T-Sing
False
Sing...0...18295
T-Dancer
True
Dancer...0...18295
game over...
Sing...1...18295
Dancer...1...18295
Sing...2...18295
Dancer...2...18295
方式二:
[root@localhost demo01]# chmod a+x aa.py
[root@localhost demo01]# ./aa.py
T-Sing
False
Sing...0...18310
T-Dancer
True
Dancer...0...18310
game over...
Sing...1...18310
Dancer...1...18310
Sing...2...18310