14,python如何创建进程并在父子进程通信
示例代码如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
 
  1. import os, sys  
  2. print "I'm going to fork now - the child will write something to a pipe, and the parent will read it back"  
  3. r, w = os.pipe()           # r,w是文件描述符, 不是文件对象  
  4. pid = os.fork()  
  5. if pid:  
  6.     # 父进程  
  7.     os.close(w)           # 关闭一个文件描述符  
  8.     r = os.fdopen(r)      # 将r转化为文件对象  
  9.     print "parent: reading"  
  10.     txt = r.read()  
  11.     os.waitpid(pid, 0)   # 确保子进程被撤销  
  12. else:  
  13.     # 子进程               
  14.     os.close(r)  
  15.     w = os.fdopen(w, 'w')  
  16.     print "child: writing"  
  17.     w.write("here's some text from the child")  
  18.     w.close()  
  19.     print "child: closing"  
  20.     sys.exit(0)  
  21. print "parent: got it; text =", txt  


 15 py2exe

点击打开链接

 

16,python如何判断当前操作系统的类型:

def UsePlatform():
  sysstr = platform.system()
  if(sysstr =="Windows"):
    print ("Call Windows tasks")
  elif(sysstr == "Linux"):
    print ("Call Linux tasks")
  else:
    print ("Other System tasks")

 

17,python文件夹复制:

以下转自:http://www.oschina.net/code/snippet_16840_1654

[python] view plaincopy在CODE上查看代码片派生到我的代码片
 
    1. sourceDir = r"\\192.168.3.250\mmtimages"     
    2.   
    3. 10 targetDir = r"D:\mmtimages"     
    4.   
    5. 11 copyFileCounts = 0     
    6.   
    7. 12        
    8.   
    9. 13 def copyFiles(sourceDir, targetDir):      
    10.   
    11. 14     global copyFileCounts      
    12.   
    13. 15     print sourceDir      
    14.   
    15. 16     print u"%s 当前处理文件夹%s已处理%s 个文件" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), sourceDir,copyFileCounts)      
    16.   
    17. 17     for f in os.listdir(sourceDir):      
    18.   
    19. 18         sourceF = os.path.join(sourceDir, f)      
    20.   
    21. 19         targetF = os.path.join(targetDir, f)      
    22.   
    23. 20                      
    24.   
    25. 21         if os.path.isfile(sourceF):      
    26.   
    27. 22             #创建目录      
    28.   
    29. 23             if not os.path.exists(targetDir):      
    30.   
    31. 24                 os.makedirs(targetDir)      
    32.   
    33. 25             copyFileCounts += 1     
    34.   
    35. 26                    
    36.   
    37. 27             #文件不存在,或者存在但是大小不同,覆盖      
    38.   
    39. 28             if not os.path.exists(targetF) or (os.path.exists(targetF) and (os.path.getsize(targetF) != os.path.getsize(sourceF))):      
    40.   
    41. 29                 #2进制文件      
    42.   
    43. 30                 open(targetF, "wb").write(open(sourceF, "rb").read())      
    44.   
    45. 31                 print u"%s %s 复制完毕" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)      
    46.   
    47. 32             else:      
    48.   
    49. 33                 print u"%s %s 已存在,不重复复制" %(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())), targetF)      
    50.   
    51. 34                
    52.   
    53. 35         if os.path.isdir(sourceF):      
    54.   
    55. 36             copyFiles(sourceF, targetF)    
posted on 2015-01-20 20:28  知了112  阅读(148)  评论(0编辑  收藏  举报