windows版python下载:
https://pan.baidu.com/s/1dsAPp0C9PJUF73kFDdAzXQ
安装时勾选pip和Add python.exe to Path。
windows版pycharm下载:
https://pan.baidu.com/s/1DV81hxSsodtukUHNW-Bzgg
一些基础知识:
1、if __name__ == '__main__'的意思是:
当.py文件被直接运行时,if __name__ == '__main__'之下的代码块将被运行;当.py文件以模块形式被导入时,if __name__ == '__main__'之下的代码块不被运行。
2、在python执行py文件
- import sys #引入sys库体
- sys.path.append("C://myPython") #往系统路径中加入自己存放py文件的地址
然后就可以开始通过import的方法导入相关的方法和内容了 - from test import * #从test.py文件中加载所有的内容。
文件读写:
with open(r'f: est 1.txt','r') as f:
print f.read() 读
f.close() 关闭
with相当于try...catch;
按行读:
with open(r'f: est 1.txt','r') as f:
for l in f.readlines():
print l.strip()
f.close()
写文件:
with open(r'f: est 1.txt','w') as f:
f.write('abcde')
f.close()
序列化:
import cPickle as pickle 引入包
with open(r'f: est 1.txt', 'wb') as f:
dic = 10
pickle.dump(dic, f)
f.close()
with open(r'f: est 1.txt', 'rb') as f:
aa = pickle.load(f)
print(aa)
f.close()
多线程:
import random import time,threading def thread_run(urls): for url in urls: print "%s ---->>>> %s"%(threading.current_thread().name,url) time.sleep(random.random()) print "thread %s end..."%threading.current_thread().name
t1 = threading.Thread(target=thread_run,name="Thread_1",args=(["url_1","url_2","url_3"],)) t2 = threading.Thread(target=thread_run,name="Thread_1",args=(["url_4","url_5","url_6"],)) t1.start() t2.start() t1.join() t2.join() print "end....."
//join是主线程等待子线程
import random import threading import time class myThread(threading.Thread): def __init__(self,name,urls): threading.Thread.__init__(self,name=name) self.urls = urls def run(self): for url in self.urls: print "%s ---->>> %s" %(threading.current_thread().name,url) print "%s ended..."%threading.current_thread().name t1 = myThread(name="Thread_1",urls=["url_1","url_2","url_3"]) t2 = myThread(name="Thread_2",urls=["url_4","url_5","url_6"]) t1.start() t2.start() t1.join() t2.join() print "%s end...." %threading.current_thread().name
线程同步:
import random import threading import time mylock = threading.RLock() class myThread(threading.Thread): def __init__(self,name,urls): threading.Thread.__init__(self,name=name) self.urls = urls def run(self): for url in self.urls: mylock.acquire() print "%s ---->>> %s --" %(threading.current_thread().name,url) mylock.release() t1 = myThread(name="Thread_1",urls=["url_1","url_2","url_3"]) t2 = myThread(name="Thread_2",urls=["url_4","url_5","url_6"]) t1.start() t2.start() t1.join() t2.join() print "%s end...." %threading.current_thread().name