python3_标准库_os库(模块)方法总结

1.常用方法介绍

# coding:utf-8
import json
import scrapy
import os
import stat
import sys


# 1.检验权限模式
# 1.def access(path, mode)
# 1.path:字符串(string)或者字节(bytes)
# 1.mode:os.F_OK(测试文件是否存在0)
# 1.        os.R_OK(测试是否可读4) | os.W_OK(测试是否可写2) | os.X_OK(测试是否可执行1)
print(os.access("E:\\Python\\课程代码\\a.txt", os.F_OK))
print("*" * 98)

# 2.返回当前工作目录
# 2.def getcwd(*args, **kwargs)
print(os.getcwd())
print("*" * 98)

# 3.改变当前工作目录
# 3.def chdir(path)
# 3.def chdir(*args, **kwargs)
# 3.path常为字符串。某些平台也可为一个打开的文件描述符。路径错误时引发FileNotFoundError异常
print(os.chdir("E:\\Python\\课程代码"))
print(os.chdir("E:\\Python\\课程代码\\test"))
print(os.getcwd())
print("*" * 98)

# 4.以mode权限创建一个名为path的文件夹
# 4.def mkdir(path, mode)
# 4.def mkdir(*args, **kwargs)
# 4.默认的 mode 是 0777 (八进制),且该参数在windows平台被忽略可不填
print(os.mkdir("E:\\Python\\课程代码\\test\\mx_xin"))
print("*" * 98)

# 5.删除path指定的空目录,若目录非空则抛出OSError异常
# 5.def rmdir(path)
# 5.def rmdir(*args, **kwargs)
# 5.目录非空抛出OSError异常;目录不存在抛出FileNotFoundError异常
print(os.rmdir("E:\\Python\\课程代码\\test\\mx_xin"))
print(os.rmdir("E:\\Python\\课程代码\\test\\ma_xin"))
print("*" * 98)

# 6.打开一个文件,并设置需要的打开选项,返回一个整数型的文件描述符
# 6.def open(file, flags, mode)
# 6.def open(*args, **kwargs)
# 6.file:要打开的文件 | flags:os.O_RDONLY: 以只读的方式打开   os.O_WRONLY: 以只写的方式打开
#                           os.O_RDWR : 以读写的方式打开    os.O_NONBLOCK: 打开时不阻塞
#                           os.O_APPEND: 以追加的方式打开   os.O_CREAT: 创建并打开一个新文件
# 6.多个权限之间可用 | 分隔(os.O_RDONLY | os.O_WRONLY)
fd = os.open("E:\\Python\\课程代码\\test\\mx.py", osos.O_RDWR)
print(fd)
print("*" * 98)

# 7.写入bytes对象到文件描述符fd中,返回实际写入的文件描述符长度
# 7.def write(fd, bytes(str, encoding="utf-8"))
# 7.def write(*args, **kwargs)
print(os.write(fd, bytes("str='hello world'", encoding="utf-8")))

# 8.从文件描述符fd中读取最多n个字节,返回包含读取字节的bytes对象(字符串)(fd起始位置位于末尾,返回空字符串)
# 8.def read(fd, n)
# 8.def read(*args, **kwargs)
ret = os.read(fd, 20)
ret2 = os.read(fd, 5)
print(ret)
print(ret2)
print(type(ret))
print("*" * 98)

# 9.关闭指定的文件描述符fd
# 9.def close(fd)
# 9.def def close(*args, **kwargs)
# 9.无返回值
print(os.close(fd))
print("*" * 98)

# 10.更改文件或者目录权限
# 10.def chmod(path, mode)
# 10.def chmod(*args, **kwargs)
# 10.无返回值
# 10.删除或添加目录中的文件必需同时具有写和执行权限
print(os.chmod("./mx.py", stat.S_IXGRP | stat.S_IWGRP))

# 11.修改当前进程的更目录为指定目录(需要管理员权限)
# 11.def chroot(path)
# 11.无返回值
print(os.mkdir("./ma_xin"))
print(os.chroot("./mx_xin"))

# 12.关闭所有在fd_low, fd_high之间的文件描述符
# 12.def closerange(fd_low, fd_high)
# 12.def closerange(*args, **kwargs)
# 12.文件描述符有误,抛出OSError异常
# 12.无返回值
print(os.closerange(0, 2))

# 13.复制文件描述符fd
fd1 = os.dup(fd)
os.dup2(fd, fd2)

# 14.删除路径为path的文件。如果path 是一个文件夹,将抛出OSError
# 14.def remove(path)
# 14.def remove(*args, **kwargs)
# 14.无返回值
# 14.os.removedirs(path)递归删除文件目录
print(os.remove("./remove_mx.py"))

# 15.以列表形式返回指定的文件夹包含的文件或文件夹的名字的列表(按照字母排序)
# 15.def listdir(path)
# 15.def listdir(*args, **kwargs)
# 15.
dir_file = os.listdir(".")
for file in dir_file:
    print(file)

# 16.重命名文件或者目录(src_name -> dst_name)
# 16.def rename(src_name, dst_name)
# 16.无返回值
# 16.def renames(old, new)递归重命名目录或文件
print(os.rename("./ma_xin", "./mxin"))

2.其他方法

文章1:https://blog.csdn.net/u011077672/article/details/48198299

文章2:http://www.runoob.com/python/os-file-methods.html


猜你喜欢

转载自blog.csdn.net/admin_maxin/article/details/80047783