Latest web development tutorials

파이썬 os.pipe () 메소드

파이썬 파일 (파일) 방법 파이썬 OS 파일 / 디렉토리 방법


개요

os.pipe () 메소드가 파이프 라인을 만드는 데 사용되는 읽기 및 쓰기되는 파일 기술자 (R, w)의 한 쌍을 돌려줍니다.

문법

다음과 같이파이프 () 메서드 구문은 다음과 같습니다

os.pipe()

매개 변수

  • 아니

반환 값

파일 기술자의 권리를 돌려줍니다.

다음의 예는 파이프 () 메소드를 사용하는 방법을 보여줍니다 :

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import os, sys

print "The child will write text to a pipe and "
print "the parent will read the text written by child..."

# file descriptors r, w for reading and writing
r, w = os.pipe() 

processid = os.fork()
if processid:
    # This is the parent process 
    # Closes file descriptor w
    os.close(w)
    r = os.fdopen(r)
    print "Parent reading"
    str = r.read()
    print "text =", str   
    sys.exit(0)
else:
    # This is the child process
    os.close(r)
    w = os.fdopen(w, 'w')
    print "Child writing"
    w.write("Text written by child...")
    w.close()
    print "Child closing"
    sys.exit(0)

위 프로그램의 출력은 다음과 같습니다

The child will write text to a pipe and
the parent will read the text written by child...
Parent reading
Child writing
Child closing
text = Text written by child...

파이썬 파일 (파일) 방법 파이썬 OS 파일 / 디렉토리 방법