导航:首页 > 编程语言 > pythonosread

pythonosread

发布时间:2023-02-07 00:39:32

python os.system、os.popen、subprocess.Popen的区别

1、使用os.system("cmd")

这是最简单的一种方法,其执行过程中会输出显示cmd命令执行的信息。

例如:print os.system("mkdir test") >>>输出:0

可以看到结果打印出0,表示命令执行成功;否则表示失败(再次执行该命令,输出:子目录或文件 test 已经存在。1)。

2、使用os.popen("cmd")

通过os.popen()返回的是 file read 的对象,对其进行读取read()操作可以看到执行的输出

例如:print os.popen("adb shell ls /sdcard/ | findstr aa.png").read() >>> 输出:aa.png(若aa.png存在,否则输出为空)

3、subprocess.Popen("cmd")

subprocess模块被推荐用来替换一些老的模块和函数,如:os.system、os.spawn*、os.popen*等

subprocess模块目的是 启动一个新的进程并与之通信 ,最常用是定义类Popen,使用Popen可以创建进程,并与进程进行复杂的交互。其函数原型为:

classsubprocess.Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)

Popen非常强大,支持多种参数和模式,通过其构造函数可以看到支持很多参数。但Popen函数存在缺陷在于, 它是一个阻塞的方法 ,如果运行cmd命令时产生内容非常多,函数就容易阻塞。另一点, Popen方法也不会打印出cmd的执行信息 。

以下罗列常用到的参数:

args :这个参数必须是 字符串 或者是一个由 字符串成员的列表 。其中如果是一个字符串行表的话,那第一个成员为要运行的程序的路径以及程序名称;从第二个成员开始到最后一个成员为运行这个程序需要输入的参数。这与popen中是一样的。

bufsize: 一般使用比较少,略过。

executable: 指定要运行的程序,这个一般很少用到,因为要指定运行的程序在args中已经指定了。 stdin,stdout ,stderr: 分别代表程序的标准输入、标准输出、标准错误处理。可以选择的值有 PIPE , 已经存在的打开的文件对象 和 NONE 。若stdout是文件对象的话,要确保文件对象是处于打开状态。

shell:shell参数根据要执行的命令情况来定,如果将参数shell设为True,executable将指定程序使用的shell。在windows平台下,默认的shell由COMSPEC环境变量来指定。

② python os.rename 报错OSError: [Errno 2] No such file or directory

for APK in apklist:
portion = os.path.splitext(APK)

if portion[1] == ".apk":
newname = portion[0] + ".zip"
os.rename(os.path.join(path,APK),newname) #os.rename是在当前目录下操作,是不是得加上path这个路径

③ python 基础教程

运算

a = 21
b = 10
c = 0

c = a + b
print "1 - c 的值为:", c

c = a - b
print "2 - c 的值为:", c

c = a * b
print "3 - c 的值为:", c

c = a / b
print "4 - c 的值为:", c

c = a % b
print "5 - c 的值为:", c

a = 2
b = 3
c = a**b
print "6 - c 的值为:", c

a = 10
b = 5
c = a//b
print "7 - c 的值为:", c

python比较

a = 21
b = 10
c = 0

if ( a == b ):
print "1 - a 等于 b"
else:
print "1 - a 不等于 b"

if ( a != b ):
print "2 - a 不等于 b"
else:
print "2 - a 等于 b"

if ( a <> b ):
print "3 - a 不等于 b"
else:
print "3 - a 等于 b"

if ( a < b ):
print "4 - a 小于 b"
else:
print "4 - a 大于等于 b"

if ( a > b ):
print "5 - a 大于 b"
else:
print "5 - a 小于等于 b"

a = 5
b = 20
if ( a <= b ):
print "6 - a 小于等于 b"
else:
print "6 - a 大于 b"

if ( b >= a ):
print "7 - b 大于等于 a"
else:
print "7 - b 小于 a"

赋值

a = 21
b = 10
c = 0

c = a + b
print "1 - c 的值为:", c

c += a
print "2 - c 的值为:", c

c *= a
print "3 - c 的值为:", c

c /= a
print "4 - c 的值为:", c

c = 2
c %= a
print "5 - c 的值为:", c

c **= a
print "6 - c 的值为:", c

c //= a
print "7 - c 的值为:", c

逻辑运算符:

a = 10
b = 20

if ( a and b ):
print "1 - 变量 a 和 b 都为 true"
else:
print "1 - 变量 a 和 b 有一个不为 true"

if ( a or b ):
print "2 - 变量 a 和 b 都为 true,或其中一个变量为 true"
else:
print "2 - 变量 a 和 b 都不为 true"

a = 0
if ( a and b ):
print "3 - 变量 a 和 b 都为 true"
else:
print "3 - 变量 a 和 b 有一个不为 true"

if ( a or b ):
print "4 - 变量 a 和 b 都为 true,或其中一个变量为 true"
else:
print "4 - 变量 a 和 b 都不为 true"

if not( a and b ):
print "5 - 变量 a 和 b 都为 false,或其中一个变量为 false"
else:
print "5 - 变量 a 和 b 都为 true"

in,not in

a = 10
b = 20
list = [1, 2, 3, 4, 5 ];

if ( a in list ):
print "1 - 变量 a 在给定的列表中 list 中"
else:
print "1 - 变量 a 不在给定的列表中 list 中"

if ( b not in list ):
print "2 - 变量 b 不在给定的列表中 list 中"
else:
print "2 - 变量 b 在给定的列表中 list 中"

a = 2
if ( a in list ):
print "3 - 变量 a 在给定的列表中 list 中"
else:
print "3 - 变量 a 不在给定的列表中 list 中"

条件

flag = False
name = 'luren'
if name == 'python': # 判断变量否为'python'
flag = True # 条件成立时设置标志为真
print 'welcome boss' # 并输出欢迎信息
else:
print name

num = 5
if num == 3: # 判断num的值
print 'boss'
elif num == 2:
print 'user'
elif num == 1:
print 'worker'
elif num < 0: # 值小于零时输出
print 'error'
else:
print 'roadman' # 条件均不成立时输出

循环语句:

count = 0
while (count < 9):
print 'The count is:', count
count = count + 1

print "Good bye!"

i = 1
while i < 10:
i += 1
if i%2 > 0: # 非双数时跳过输出
continue
print i # 输出双数2、4、6、8、10

i = 1
while 1: # 循环条件为1必定成立
print i # 输出1~10
i += 1
if i > 10: # 当i大于10时跳出循环
break

for letter in 'Python': # 第一个实例
print '当前字母 :', letter

fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print '当前水果 :', fruit

print "Good bye!"

获取用户输入:raw_input

var = 1
while var == 1 : # 该条件永远为true,循环将无限执行下去
num = raw_input("Enter a number :")
print "You entered: ", num

print "Good bye!"

range,len

fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print '当前水果 :', fruits[index]

print "Good bye!"

python数学函数:
abs,cell,cmp,exp,fabs,floor,log,log10,max,min,mod,pow,round,sqrt

randrange

访问字符串的值

var1 = 'Hello World!'
var2 = "Python Runoob"

print "var1[0]: ", var1[0]
print "var2[1:5]: ", var2[1:5]

转义字符

格式化输出
print "My name is %s and weight is %d kg!" % ('Zara', 21)

字符串函数:

添加元素

list = [] ## 空列表
list.append('Google') ## 使用 append() 添加元素
list.append('Runoob')
print list

删除元素

list1 = ['physics', 'chemistry', 1997, 2000]

print list1
del list1[2]
print "After deleting value at index 2 : "
print list1

列表操作

列表方法

删除字典

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

del dict['Name']; # 删除键是'Name'的条目
dict.clear(); # 清空词典所有条目
del dict ; # 删除词典

print "dict['Age']: ", dict['Age'];
print "dict['School']: ", dict['School'];

字典的函数:

当前时间戳:
import time
time.time()

格式化日期输出

import time

print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())

print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())

a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

获取某个月日历:calendar

import calendar

cal = calendar.month(2016, 1)
print "以下输出2016年1月份的日历:"
print cal

当前日期和时间

import datetime
i = datetime.datetime.now()
print ("当前的日期和时间是 %s" % i)
print ("ISO格式的日期和时间是 %s" % i.isoformat() )
print ("当前的年份是 %s" %i.year)
print ("当前的月份是 %s" %i.month)
print ("当前的日期是 %s" %i.day)
print ("dd/mm/yyyy 格式是 %s/%s/%s" % (i.day, i.month, i.year) )
print ("当前小时是 %s" %i.hour)
print ("当前分钟是 %s" %i.minute)
print ("当前秒是 %s" %i.second)

不定长参数:*

lambda:匿名函数

def....

python模块搜索路径

获取用户输入

str = raw_input("请输入:")
print "你输入的内容是: ", str

input可以接收表达式

open参数

write要自己添加换行符

读取10个字符

重命名:os.rename
os.remove
os.mkdir os.chdir
os.getcwd
os.rmdir

open参数

file的方法

异常:

try:
fh = open("testfile", "w")
fh.write("这是一个测试文件,用于测试异常!!")
except IOError:
print "Error: 没有找到文件或读取文件失败"
else:
print "内容写入文件成功"
fh.close()

try:
fh = open("testfile", "w")
fh.write("这是一个测试文件,用于测试异常!!")
finally:
print "Error: 没有找到文件或读取文件失败"

用户自定义异常:

os 模块提供了非常丰富的方法用来处理文件和目录。常用的方法如下表所示:

| 序号 | 方法及描述 |
| 1 |

os.access(path, mode)

检验权限模式 |
| 2 |

os.chdir(path)

改变当前工作目录 |
| 3 |

os.chflags(path, flags)

设置路径的标记为数字标记。 |
| 4 |

os.chmod(path, mode)

更改权限 |
| 5 |

os.chown(path, uid, gid)

更改文件所有者 |
| 6 |

os.chroot(path)

改变当前进程的根目录 |
| 7 |

os.close(fd)

关闭文件描述符 fd |
| 8 |

os.closerange(fd_low, fd_high)

关闭所有文件描述符,从 fd_low (包含) 到 fd_high (不包含), 错误会忽略 |
| 9 |

os.p(fd)

复制文件描述符 fd |
| 10 |

os.p2(fd, fd2)

将一个文件描述符 fd 复制到另一个 fd2 |
| 11 |

os.fchdir(fd)

通过文件描述符改变当前工作目录 |
| 12 |

os.fchmod(fd, mode)

改变一个文件的访问权限,该文件由参数fd指定,参数mode是Unix下的文件访问权限。 |
| 13 |

os.fchown(fd, uid, gid)

修改一个文件的所有权,这个函数修改一个文件的用户ID和用户组ID,该文件由文件描述符fd指定。 |
| 14 |

os.fdatasync(fd)

强制将文件写入磁盘,该文件由文件描述符fd指定,但是不强制更新文件的状态信息。 |
| 15 |

os.fdopen(fd[, mode[, bufsize]])

通过文件描述符 fd 创建一个文件对象,并返回这个文件对象 |
| 16 |

os.fpathconf(fd, name)

返回一个打开的文件的系统配置信息。name为检索的系统配置的值,它也许是一个定义系统值的字符串,这些名字在很多标准中指定(POSIX.1, Unix 95, Unix 98, 和其它)。 |
| 17 |

os.fstat(fd)

返回文件描述符fd的状态,像stat()。 |
| 18 |

os.fstatvfs(fd)

返回包含文件描述符fd的文件的文件系统的信息,像 statvfs() |
| 19 |

os.fsync(fd)

强制将文件描述符为fd的文件写入硬盘。 |
| 20 |

os.ftruncate(fd, length)

裁剪文件描述符fd对应的文件, 所以它最大不能超过文件大小。 |
| 21 |

os.getcwd()

返回当前工作目录 |
| 22 |

os.getcw()

返回一个当前工作目录的Unicode对象 |
| 23 |

os.isatty(fd)

如果文件描述符fd是打开的,同时与tty(-like)设备相连,则返回true, 否则False。 |
| 24 |

os.lchflags(path, flags)

设置路径的标记为数字标记,类似 chflags(),但是没有软链接 |
| 25 |

os.lchmod(path, mode)

修改连接文件权限 |
| 26 |

os.lchown(path, uid, gid)

更改文件所有者,类似 chown,但是不追踪链接。 |
| 27 |

os.link(src, dst)

创建硬链接,名为参数 dst,指向参数 src |
| 28 |

os.listdir(path)

返回path指定的文件夹包含的文件或文件夹的名字的列表。 |
| 29 |

os.lseek(fd, pos, how)

设置文件描述符 fd当前位置为pos, how方式修改: SEEK_SET 或者 0 设置从文件开始的计算的pos; SEEK_CUR或者 1 则从当前位置计算; os.SEEK_END或者2则从文件尾部开始. 在unix,Windows中有效 |
| 30 |

os.lstat(path)

像stat(),但是没有软链接 |
| 31 |

os.major(device)

从原始的设备号中提取设备major号码 (使用stat中的st_dev或者st_rdev field)。 |
| 32 |

os.makedev(major, minor)

以major和minor设备号组成一个原始设备号 |
| 33 |

os.makedirs(path[, mode])

递归文件夹创建函数。像mkdir(), 但创建的所有intermediate-level文件夹需要包含子文件夹。 |
| 34 |

os.minor(device)

从原始的设备号中提取设备minor号码 (使用stat中的st_dev或者st_rdev field )。 |
| 35 |

os.mkdir(path[, mode])

以数字mode的mode创建一个名为path的文件夹.默认的 mode 是 0777 (八进制)。 |
| 36 |

os.mkfifo(path[, mode])

创建命名管道,mode 为数字,默认为 0666 (八进制) |
| 37 |

os.mknod(filename[, mode=0600, device])
创建一个名为filename文件系统节点(文件,设备特别文件或者命名pipe)。

|
| 38 |

os.open(file, flags[, mode])

打开一个文件,并且设置需要的打开选项,mode参数是可选的 |
| 39 |

os.openpty()

打开一个新的伪终端对。返回 pty 和 tty的文件描述符。 |
| 40 |

os.pathconf(path, name)

返回相关文件的系统配置信息。 |
| 41 |

os.pipe()

创建一个管道. 返回一对文件描述符(r, w) 分别为读和写 |
| 42 |

os.popen(command[, mode[, bufsize]])

从一个 command 打开一个管道 |
| 43 |

os.read(fd, n)

从文件描述符 fd 中读取最多 n 个字节,返回包含读取字节的字符串,文件描述符 fd对应文件已达到结尾, 返回一个空字符串。 |
| 44 |

os.readlink(path)

返回软链接所指向的文件 |
| 45 |

os.remove(path)

删除路径为path的文件。如果path 是一个文件夹,将抛出OSError; 查看下面的rmdir()删除一个 directory。 |
| 46 |

os.removedirs(path)

递归删除目录。 |
| 47 |

os.rename(src, dst)

重命名文件或目录,从 src 到 dst |
| 48 |

os.renames(old, new)

递归地对目录进行更名,也可以对文件进行更名。 |
| 49 |

os.rmdir(path)

删除path指定的空目录,如果目录非空,则抛出一个OSError异常。 |
| 50 |

os.stat(path)

获取path指定的路径的信息,功能等同于C API中的stat()系统调用。 |
| 51 |

os.stat_float_times([newvalue])
决定stat_result是否以float对象显示时间戳

|
| 52 |

os.statvfs(path)

获取指定路径的文件系统统计信息 |
| 53 |

os.symlink(src, dst)

创建一个软链接 |
| 54 |

os.tcgetpgrp(fd)

返回与终端fd(一个由os.open()返回的打开的文件描述符)关联的进程组 |
| 55 |

os.tcsetpgrp(fd, pg)

设置与终端fd(一个由os.open()返回的打开的文件描述符)关联的进程组为pg。 |
| 56 |

os.tempnam([dir[, prefix]])

返回唯一的路径名用于创建临时文件。 |
| 57 |

os.tmpfile()

返回一个打开的模式为(w+b)的文件对象 .这文件对象没有文件夹入口,没有文件描述符,将会自动删除。 |
| 58 |

os.tmpnam()

为创建一个临时文件返回一个唯一的路径 |
| 59 |

os.ttyname(fd)

返回一个字符串,它表示与文件描述符fd 关联的终端设备。如果fd 没有与终端设备关联,则引发一个异常。 |
| 60 |

os.unlink(path)

删除文件路径 |
| 61 |

os.utime(path, times)

返回指定的path文件的访问和修改的时间。 |
| 62 |

os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])

输出在文件夹中的文件名通过在树中游走,向上或者向下。 |
| 63 |

os.write(fd, str)

写入字符串到文件描述符 fd中. 返回实际写入的字符串长度 |

④ python语句 os.system os.popen什么意思

os.system() 和os.popen()的区别
返回的数据不同

1 os.system(“ls") 返回0
但是这样是无法获得到输出和返回值的
继续 Google,之后学会了 os.popen()。 view sourceprint?
a... output = os.popen('cat /proc/cpuinfo')
b... print output.read()
os.system() returns the (encoded) process exit value. 0 means success:
输出0为正确运行。1为出现异常。
如果你想得到标准输出,可以使用 subprocess.check_output() 来代替这个方法
x = subprocess.check_output(['whoami'])

⑤ 如何学习python的os模块

一、os模块概述

Python os模块包含普遍的操作系统功能。如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的。(一语中的)

二、常用方法

1、os.name

输出字符串指示正在使用的平台。如果是window 则用'nt'表示,对于linux/Unix用户,它是'posix'。

2、os.getcwd()

函数得到当前工作目录,即当前Python脚本工作的目录路径。

3、os.listdir()

返回指定目录下的所有文件和目录名。

>>> os.listdir(os.getcwd())
['Django', 'DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'MySQL-python-wininst.log', 'NEWS.txt', 'PIL-wininst.log', 'python.exe', 'pythonw.exe', 'README.txt', 'RemoveMySQL-python.exe', 'RemovePIL.exe', 'Removesetuptools.exe', 'Scripts', 'setuptools-wininst.log', 'tcl', 'Tools', 'w9xpopen.exe']
>>>

4、os.remove()

删除一个文件。

5、os.system()

运行shell命令。

>>> os.system('dir')
0
>>> os.system('cmd') #启动dos

6、os.sep 可以取代操作系统特定的路径分割符。

7、os.linesep字符串给出当前平台使用的行终止符

>>> os.linesep
'\r\n' #Windows使用'\r\n',Linux使用'\n'而Mac使用'\r'。
>>> os.sep
'\\' #Windows
>>>

8、os.path.split()

函数返回一个路径的目录名和文件名

>>> os.path.split('C:\\Python25\\abc.txt')
('C:\\Python25', 'abc.txt')

9、os.path.isfile()和os.path.isdir()函数分别检验给出的路径是一个文件还是目录。

>>> os.path.isdir(os.getcwd())
True
>>> os.path.isfile('a.txt')
False

10、os.path.exists()函数用来检验给出的路径是否真地存在

>>> os.path.exists('C:\\Python25\\abc.txt')
False
>>> os.path.exists('C:\\Python25')
True
>>>

11、os.path.abspath(name):获得绝对路径

12、os.path.normpath(path):规范path字符串形式

13、os.path.getsize(name):获得文件大小,如果name是目录返回0L

14、os.path.splitext():分离文件名与扩展名

>>> os.path.splitext('a.txt')
('a', '.txt')

15、os.path.join(path,name):连接目录与文件名或目录

>>> os.path.join('c:\\Python','a.txt')
'c:\\Python\\a.txt'
>>> os.path.join('c:\\Python','f1')
'c:\\Python\\f1'
>>>

16、os.path.basename(path):返回文件名

>>> os.path.basename('a.txt')
'a.txt'
>>> os.path.basename('c:\\Python\\a.txt')
'a.txt'
>>>

17、os.path.dirname(path):返回文件路径

>>> os.path.dirname('c:\\Python\\a.txt')
'c:\\Python'

⑥ python,os.popen 打包后出现问题

你打包成exe后,命令行应该是pyinstller -Fw xxx.py
你加上了w参数也就是把console设置成了flase;那么os.popen()或者subprocess.popen()执行的时候没有载体,你只有把console设置成true,也就是命令改为pyinstaller -F xxx.py,这样你的os.popen()可执行,也能获得返回值。

⑦ 如何用python编程实现lspci的功能

lspci 是个linux 命令,可以用python 的os.popen/os.system/subprocess 等更使用系统shell

举个栗子:

importos
#os.system
os.system("lspci")

#os.popen
os.popen("lspci").read()

Python 跟shell 沟通有很多种方式, 可以多查查pydoc。 希望有帮助

⑧ 如何用Python os.path.walk方法遍历搜索文件内容的操作详解

文中使用到了Python os模块和Python sys模块,这两个模块具体的使用方法请参考玩蛇网相关文章阅读。
Python os.path.walk方法遍历文件搜索内容方法代码如下:
?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

import os, sys
#代码中需要用到的方法模块导入

listonly = False

skipexts = ['.gif', '.exe', '.pyc', '.o', '.a','.dll','.lib','.pdb','.mdb'] # ignore binary files

def visitfile(fname, searchKey):
global fcount, vcount
try:
if not listonly:
if os.path.splitext(fname)[1] in skipexts:
pass
elif open(fname).read().find(searchKey) != -1:
print'%s has %s' % (fname, searchKey)
fcount += 1
except: pass
vcount += 1

#www.iplaypy.com

def visitor(args, directoryName,filesInDirectory):
for fname in filesInDirectory:
fpath = os.path.join(directoryName, fname)
if not os.path.isdir(fpath):
visitfile(fpath,args)

def searcher(startdir, searchkey):
global fcount, vcount
fcount = vcount = 0
os.path.walk(startdir, visitor, searchkey)

if __name__ == '__main__':
root=raw_input("type root directory:")
key=raw_input("type key:")
searcher(root,key)
print 'Found in %d files, visited %d' % (fcount, vcount)

⑨ 如何操作 python os.popen的返回

ret = os.popen("ls").read()
但是,一些命令是不会输出消息的,所以调用read的时候会阻塞,你需要注意一下

⑩ python编程中 os.mkfifo()和os.mknod()函数具体用法最好有例子,里面参数具体怎么配置就能创建管道或节

mkfifo函数使用
[code]mkfifo(建立实名管道)
相关函数
pipe,popen,open,umask
表头文件
#include
#include
定义函数
int mkfifo(const char * pathname,mode_t mode);
函数说明
mkfifo() 会依参数pathname建立特殊的FIFO文件,该文件必须不存在,而参数mode为该文件的权限(mode%~umask),因此 umask值也会影响到FIFO文件的权限。Mkfifo()建立的FIFO文件其他进程都可以用读写一般文件的方式存取。当使用open()来打开 FIFO文件时,O_NONBLOCK旗标会有影响
1、当使用O_NONBLOCK 旗标时,打开FIFO 文件来读取的操作会立刻返回,但是若还没有其他进程打开FIFO 文件来读取,则写入的操作会返回ENXIO 错误代码。
2、没有使用O_NONBLOCK 旗标时,打开FIFO 来读取的操作会等到其他进程打开FIFO文件来写入才正常返回。同样地,打开FIFO文件来写入的操作会等到其他进程打开FIFO 文件来读取后才正常返回。
返回值
若成功则返回0,否则返回-1,错误原因存于errno中。
错误代码
EACCESS 参数pathname所指定的目录路径无可执行的权限
EEXIST 参数pathname所指定的文件已存在。
ENAMETOOLONG 参数pathname的路径名称太长。
ENOENT 参数pathname包含的目录不存在
ENOSPC 文件系统的剩余空间不足
ENOTDIR 参数pathname路径中的目录存在但却非真正的目录。
EROFS 参数pathname指定的文件存在于只读文件系统内。

示例1:
#include
#include
#include
#include

int main(void)
{
char buf[80];
int fd;
unlink( "zieckey_fifo" );
mkfifo( "zieckey_fifo", 0777 );

if ( fork() > 0 )
{
char s[] = "Hello!\n";
fd = open( "zieckey_fifo", O_WRONLY );
write( fd, s, sizeof(s) );
//close( fd );
}
else
{
fd = open( "zieckey_fifo", O_RDONLY );
read( fd, buf, sizeof(buf) );
printf("The message from the pipe is:%s\n", buf );
//close( fd );
}

return 0;
}
执行
hello!

示例2:
#include
#include
#include
#include
#include

int main( int argc, char **argv )
{
mode_t mode = 0666;
if ( argc !=2 )
{
printf( "Usage:[%s] fifo_filename\n", argv[0] );
return -1;
}

if (mkfifo( argv[1], mode)<0 )
{
perror( "mkfifo");
return -1;
}

return 0;
} [/code]

阅读全文

与pythonosread相关的资料

热点内容
程序员都是如何自学的 浏览:937
迅雷影院类似的网站 浏览:492
韩国电影吻戏电影推荐5个小时合集 浏览:82
有一部小说女主角叫苏 浏览:299
一女主多男主的小说在线阅读 浏览:810
电影院的人可以携带多大孩子 浏览:630
云服务器nginx集群怎么弄 浏览:741
热感1975在线观看 浏览:205
女主被几个男主囚禁,逃跑4年后回来 浏览:125
都市绝世战神叶凌天 浏览:754
拍摄指南txt百度云下载 浏览:829
女主是大学老师的小说男主是总裁 浏览:184
方舟编译器在哪升级 浏览:704
亚瑟王pdf 浏览:122
无需下载直接免费看视频的网址 浏览:903
重生红军时期的小说 浏览:670
天浴哪里有床戏 浏览:257
安卓版的电脑怎么换成苹果 浏览:712
全英文字幕电影的app 浏览:122
邵氏有关左慈的电影 浏览:390