python 一些小例子

一、shutil copytree

import shutil
shutil.copytree("a", "b")

二、os.stat(filename)文件信息

import os
import time
r = os.stat("readme.md")
print(r.st_ctime)
t = time.localtime(r.st_ctime)
print(time.strftime("%Y-%m-%d %H:%M:%S", t))

三、文件序列化

try:
    import cpickle as pickle
except Exception:
    import pickle

# dict1 = {"one": "我草", "two": 2, "three": 3}
# f = open("001.txt", "wb")
# pickle.dump(dict1, f)
# f.close()

f = open("001.txt", "rb")
cont = pickle.load(f)
f.close()
print(cont)

python 多进程,多线程multiprocessing,threading

from multiprocess import Process, Queue
import time
def adds(q, start, end):
    s = 0
    for i in range(start, end):
        s += i
    q.put(s)

if __name__ == '__main__':
    q = Queue()
    p1 = Process(target=adds, args=(q, 0, 25000000))
    p2 = Process(target=adds, args=(q, 25000000, 50000000))
    p3 = Process(target=adds, args=(q, 50000000, 75000000))
    p4 = Process(target=adds, args=(q, 75000000, 100000000))
    p1.start()
    p2.start()
    p3.start()
    p4.start()
    ssss = q.get() + q.get() + q.get() + q.get()
    p1.join()
    p2.join()
    p3.join()
    p4.join()
    print(ssss)

你可能感兴趣的:(python 一些小例子)