14,python How to create a process and communicate in a parent-child process
The sample code is as follows:
[CPP]View Plaincopy
- Import OS, sys
- Print "I ' m going to fork now-the Child would write something to a pipe, and the parent would read it back"
- r, W = Os.pipe () # r,w is a file descriptor, not a file object
- PID = Os.fork ()
- If PID:
- # Parent Process
- Os.close (W) # Close a file descriptor
- R = Os.fdopen (r) # converts r to file object
- Print "parent:reading"
- txt = r.read ()
- Os.waitpid (PID, 0) # Make sure the child process is revoked
- Else
- # Child Process
- Os.close (R)
- W = Os.fdopen (W, ' W ')
- Print "child:writing"
- W.write ("Here's some text from the child")
- W.close ()
- Print "child:closing"
- Sys.exit (0)
- 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
- SourceDir = R"\\192.168.3.250\mmtimages"
- Ten TargetDir = R"D:\mmtimages"
- Copyfilecounts = 0
- 12
- def copyfiles (SourceDir, TargetDir):
- Global Copyfilecounts
- Print SourceDir
- 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)
- + for F in Os.listdir (sourcedir):
- SOURCEF = Os.path.join (SourceDir, F)
- TARGETF = Os.path.join (TargetDir, F)
- 20
- if Os.path.isfile (SOURCEF):
- #创建目录
- If not os.path.exists (targetDir):
- Os.makedirs (TargetDir)
- Copyfilecounts + = 1
- 26
- #文件不存在, or present but different in size, covering
- If not os.path.exists (TARGETF) or (Os.path.exists (TARGETF) and (Os.path.getsize (TARGETF)! = Os.path.getsize (SOURCEF)):
- #2进制文件
- Open (TARGETF, " WB"). Write (Open (SOURCEF, "RB"). Read ())
- print u"%s%s completed"% (Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())), Targe TF)
- + Else:
- The print u"%s%s already exists, does not replicate"% (Time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ())), TARGETF)
- 34
- if Os.path.isdir (SOURCEF):
- CopyFiles (SOURCEF, TARGETF)
[Python]python Study notes (iv)