[Python]python Study notes (iv)

Source: Internet
Author: User

14,python How to create a process and communicate in a parent-child process
The sample code is as follows:

[CPP]View Plaincopy
  1. Import OS, sys
  2. Print "I ' m going to fork now-the Child would write something to a pipe, and the parent would read it back"
  3. r, W = Os.pipe () # r,w is a file descriptor, not a file object
  4. PID = Os.fork ()
  5. If PID:
  6. # Parent Process
  7. Os.close (W) # Close a file descriptor
  8. R = Os.fdopen (r) # converts r to file object
  9. Print "parent:reading"
  10. txt = r.read ()
  11. Os.waitpid (PID, 0) # Make sure the child process is revoked
  12. Else
  13. # Child Process
  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


Py2exe

Click to open link

16,python how to determine the current operating system type:

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 folder replication:

The following transfers are from: http://www.oschina.net/code/snippet_16840_1654

[Python]View Plaincopy
    1. SourceDir = R"\\192.168.3.250\mmtimages"
    2. Ten TargetDir = R"D:\mmtimages"
    3. Copyfilecounts = 0
    4. 12
    5. def copyfiles (SourceDir, TargetDir):
    6. Global Copyfilecounts
    7. Print SourceDir
    8. print u"%s currently processing folder%s has processed%s files"% (Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())), SOURC edir,copyfilecounts)
    9. + for F in Os.listdir (sourcedir):
    10. SOURCEF = Os.path.join (SourceDir, F)
    11. TARGETF = Os.path.join (TargetDir, F)
    12. 20
    13. if Os.path.isfile (SOURCEF):
    14. #创建目录
    15. If not os.path.exists (targetDir):
    16. Os.makedirs (TargetDir)
    17. Copyfilecounts + = 1
    18. 26
    19. #文件不存在, or present but different in size, covering
    20. If not os.path.exists (TARGETF) or (Os.path.exists (TARGETF) and (Os.path.getsize (TARGETF)! = Os.path.getsize (SOURCEF)):
    21. #2进制文件
    22. Open (TARGETF, " WB"). Write (Open (SOURCEF, "RB"). Read ())
    23. print u"%s%s completed"% (Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())), Targe TF)
    24. + Else:
    25. The print u"%s%s already exists, does not replicate"% (Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())), TARGETF)
    26. 34
    27. if Os.path.isdir (SOURCEF):
    28. CopyFiles (SOURCEF, TARGETF)

[Python]python Study notes (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.