python3_standard library_os library (module) method summary

1. Introduction of common methods

# coding:utf-8
import json
import scrapy
import them
import state
import sys


# 1. Check the permission mode
# 1.def access(path, mode)
# 1.path: string or bytes
# 1.mode:os.F_OK(Test whether the file exists 0)
# 1. os.R_OK(Test whether readable 4) | os.W_OK(Test whether writable 2) | os.X_OK(Test whether executable 1)
print(os.access("E:\\Python\\course code\\a.txt", os.F_OK))
print("*" * 98)

# 2. Return to the current working directory
# 2.def getcwd(*args, **kwargs)
print(os.getcwd())
print("*" * 98)

# 3. Change the current working directory
# 3.def chdir(path)
# 3.def chdir(*args, **kwargs)
# 3.path is always a string. Some platforms can also be an open file descriptor. Raise FileNotFoundError exception when path is wrong
print(os.chdir("E:\\Python\\course code"))
print(os.chdir("E:\\Python\\course code\\test"))
print(os.getcwd())
print("*" * 98)

# 4. Create a folder named path with mode permissions
# 4.def mkdir(path, mode)
# 4.def mkdir(*args, **kwargs)
# 4. The default mode is 0777 (octal), and this parameter is ignored on the Windows platform and can be left blank
print(os.mkdir("E:\\Python\\course code\\test\\mx_xin"))
print("*" * 98)

# 5. Delete the empty directory specified by path, if the directory is not empty, an OSError exception will be thrown
# 5.def rmdir(path)
# 5.def rmdir(*args, **kwargs)
# 5. If the directory is not empty, it will throw an OSError exception; if the directory does not exist, it will throw a FileNotFoundError exception
print(os.rmdir("E:\\Python\\course code\\test\\mx_xin"))
print(os.rmdir("E:\\Python\\course code\\test\\ma_xin"))
print("*" * 98)

# 6. Open a file, set the required open options, and return an integer file descriptor
# 6.def open(file, flags, mode)
# 6.def open(*args, **kwargs)
# 6.file: file to open | flags: os.O_RDONLY: open as read-only os.O_WRONLY: open as write-only
# os.O_RDWR : open for reading and writing os.O_NONBLOCK: open without blocking
# os.O_APPEND: open as append os.O_CREAT: create and open a new file
# 6. Available | Separation between multiple permissions (os.O_RDONLY | os.O_WRONLY)
fd = os.open("E:\\Python\\course code\\test\\mx.py", osos.O_RDWR)
print(fd)
print("*" * 98)

# 7. Write the bytes object to the file descriptor fd and return the actual length of the file descriptor written
# 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. Read up to n bytes from the file descriptor fd, and return a bytes object (string) containing the read bytes (the starting position of fd is at the end, and an empty string is returned)
# 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)
print("*" * 98)

# 9. Close the specified file descriptor fd
# 9.def close(fd)
# 9.def def close(*args, **kwargs)
# 9. No return value
print(os.close(fd))
print("*" * 98)

# 10. Change file or directory permissions
# 10.def chmod(path, mode)
# 10.def chmod(*args, **kwargs)
# 10. No return value
# 10. Delete or add files in a directory must have both write and execute permissions
print(os.chmod("./mx.py", stat.S_IXGRP | stat.S_IWGRP))

# 11. Modify the directory of the current process to the specified directory (requires administrator privileges)
# 11.def chroot(path)
# 11. No return value
print(os.mkdir("./ma_xin"))
print(os.chroot("./mx_xin"))

# 12. Close all file descriptors between fd_low, fd_high
# 12.def closerange(fd_low, fd_high)
# 12.def closerange(*args, **kwargs)
# 12. The file descriptor is incorrect and an OSError exception is thrown
# 12. No return value
print(os.closerange(0, 2))

# 13. Copy file descriptor fd
fd1 = os.dup (fd)
os.dup2(fd, fd2)

# 14. Delete the file whose path is path. If path is a folder, OSError will be thrown
# 14.def remove(path)
# 14.def remove(*args, **kwargs)
# 14. No return value
# 14.os.removedirs(path)Recursively delete file directories
print(os.remove("./remove_mx.py"))

# 15. Returns a list of the names of the files or folders contained in the specified folder as a list (sorted alphabetically)
# 15.def listdir(path)
# 15.def listdir(*args, **kwargs)
# 15.
dir_file = os.listdir(".")
for file in dir_file:
    print(file)

# 16. Rename a file or directory (src_name -> dst_name)
# 16.def rename(src_name, dst_name)
# 16. No return value
# 16.def renames(old, new) recursively rename a directory or file
print(os.rename("./ma_xin", "./mxin"))

2. Other methods

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

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


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325579428&siteId=291194637