OS-96

 1 print('os.access(path,mode):检验权限模式----------------------------------------------------------------')
 2 
 3 import os,sys
 4 # os.F_OK: 作为access()的mode参数,测试path是否存在。
 5 ret = os.access('E:\foo.txt',os.F_OK)
 6 print('F_OK-返回值{}'.format(ret))
 7 # os.R_OK: 包含在access()的mode参数中 , 测试path是否可读。
 8 ret = os.access('E:\foo.txt',os.R_OK)
 9 print('R_OK-返回值{}'.format(ret))
10 
11 # os.W_OK包含在access()的mode参数中 , 测试path是否可写。
12 ret = os.access('E:\foo.txt',os.W_OK)
13 print('W_OK-的返回值{}'.format(ret))
14 # os.X_OK包含在access()的mode参数中 ,测试path是否可执行。
15 ret = os.access('E:\foo.txt',os.X_OK)
16 print('X_OK-返回值{}'.format(ret))
17 
18 
19 print('os.chdir(path):改变当前工作目录------------------------------------------------')
20 '查看foo1.txt的工作目录了,并修改工作目录为新的工作目录'
21 import os,sys
22 #查看当前的工作目录
23 retval = os.getcwd()
24 print('当前的工作目录为{}'.format(retval))
25 #修改当前工作目录
26 # path = 'E:\pythonpython3.1'#如果写一个不存在的路径则提示:[WinError 2] 系统找不到指定的文件。: 'E:\python\python3.1'
27 path = 'E:\Pythonhehda'
28 os.chdir(path)
29 #查看修改后的工作目录
30 retval = os.getcwd()
31 print('修改后的工作目录了为{}'.format(retval))
32 
33 # 再修改回原工作路径
34 path1 = 'E:pythonpython3'
35 os.chdir(path1)
36 retval = os.getcwd()
37 print('把路径改回为原来的路径{}'.format(retval))
38 
39 
40 
41 print('os.closerange():关闭所有文件描述符fd------------------------------------------------')
42 import os
43 # 创建一个文件
44 f = open('E:\foo62801.txt','wb+')
45 
46 #打开文件
47 fd = os.open('foo62801.txt',os.O_RDWR|os.O_CREAT)
48 # 判断是否有写入的权限
49 ret = os.access('foo62801.txt',os.W_OK)
50 print(ret)
51 #写入字符串
52 # os.write(fd,bytes('This is test'))
53 os.write(fd,'This is test'.encode())
54 #关闭文件
55 os.closerange(fd,fd)
56 print()
57 
58 print('os.dup():用于复制文件描述符fd------------------------------------------------')
59 import os
60 # 创建一个文件
61 fd = os.open('E:\foo62802.txt',os.O_RDWR|os.O_CREAT)
62 #复制文件描述符
63 d_fd = os.dup(fd)
64 # 使用复制的文件描述符写入文件
65 os.write(d_fd,'This is a test'.encode())
66 #关闭文件
67 os.closerange(fd,d_fd)
68 print('关闭所有文件成功!')
69 
70 
71 
72 print('os.fdatasync():强制将文件写入磁盘,该文件由文件描述符fd指定,但是不强制更新文件的状态信息????????????????????????????????????')
73 import os, sys
74 
75 fd = os.open('E:\fooo.txt',os.O_RDWR|os.O_CREAT)
76 
77 os.write(fd,'This is a test'.encode())
78 #
79 os.fdatasync(fd)
80 
81 str = os.read(fd,100)
82 print('读取的字符串是:',str)
83 os.close(fd)
84 print('关闭文件成功')
85 
86 
87 
88 print('os.dup2():用于复制文件描述符fd????????????????????????????????????????????????????')
89 import os
90 f = open('foo628002.txt','a')
91 os.dup2(f.fileno(),1)
92 f.close()
93 
94 print('Runoob')
95 print('Google')
96 # 为什么没有打印输出呢?
原文地址:https://www.cnblogs.com/jpr-ok/p/9239748.html