Hàm nào sẽ trả về đường dẫn hệ thống tệp của plugin đã cho?
Mô-đun này cung cấp một cách di động để sử dụng chức năng phụ thuộc vào hệ điều hành. Nếu bạn chỉ muốn đọc hoặc ghi một tệp, hãy xem, nếu bạn muốn thao tác với các đường dẫn, hãy xem mô-đun và nếu bạn muốn đọc tất cả các dòng trong tất cả các tệp trên dòng lệnh, hãy xem mô-đun. Để tạo các tệp và thư mục tạm thời, hãy xem mô-đun và để xử lý tệp và thư mục cấp cao, hãy xem mô-đun Show
Lưu ý về tính khả dụng của các chức năng này
Note All functions in this module raise (or subclasses thereof) in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system exception os. lỗiAn alias for the built-in exception os. nameThe name of the operating system dependent module imported. The following names have currently been registered. with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)5, with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)6, with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)7 See also has a finer granularity. gives system-dependent version information The module provides detailed checks for the system’s identity File Names, Command Line Arguments, and Environment VariablesIn Python, file names, command line arguments, and environment variables are represented using the string type. On some systems, decoding these strings to and from bytes is necessary before passing them to the operating system. Python uses the to perform this conversion (see ) The are configured at Python startup by the >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 2642 function. see and members of Changed in version 3. 1. On some systems, conversion using the file system encoding may fail. In this case, Python uses the , which means that undecodable bytes are replaced by a Unicode character U+DCxx on decoding, and these are again translated to the original byte on encoding. The must guarantee to successfully decode all bytes below 128. If the file system encoding fails to provide this guarantee, API functions can raise See also the Python UTF-8 ModeNew in version 3. 7. See PEP 540 for more details. The Python UTF-8 Mode ignores the and forces the usage of the UTF-8 encoding
Note that the standard stream settings in UTF-8 mode can be overridden by (just as they can be in the default locale-aware mode) Do những thay đổi trong các API cấp thấp hơn đó, các API cấp cao hơn khác cũng thể hiện các hành vi mặc định khác nhau
Được bật nếu ngôn ngữ LC_CTYPE là os.access in os.supports_effective_ids7 hoặc os.access in os.supports_effective_ids8 khi khởi động Python (xem hàm >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 2642) Nó có thể được bật hoặc tắt bằng tùy chọn dòng lệnh và biến môi trường Nếu biến môi trường hoàn toàn không được đặt, thì trình thông dịch sẽ mặc định sử dụng cài đặt ngôn ngữ hiện tại, trừ khi ngôn ngữ hiện tại được xác định là ngôn ngữ dựa trên ASCII kế thừa (như được mô tả cho ) và tính năng cưỡng chế ngôn ngữ bị vô hiệu hóa hoặc không thành công. Ở các ngôn ngữ kế thừa như vậy, trình thông dịch sẽ mặc định bật chế độ UTF-8 trừ khi được hướng dẫn rõ ràng là không làm như vậy Chế độ Python UTF-8 chỉ có thể được bật khi khởi động Python. Giá trị của nó có thể được đọc từ Xem thêm và See also PEP 686Trăn 3. 15 sẽ làm mặc định Thông số quá trìnhCác chức năng và mục dữ liệu này cung cấp thông tin và hoạt động trên quy trình hiện tại và người dùng os. ctermid()Trả về tên tệp tương ứng với thiết bị đầu cuối kiểm soát của quy trình Unix, không phải Emscripten, không phải WASI os. môi trườngMột đối tượng trong đó các khóa và giá trị là các chuỗi đại diện cho môi trường quy trình. Ví dụ: os.chdir in os.supports_fd5 là tên đường dẫn của thư mục chính của bạn (trên một số nền tảng) và tương đương với os.chdir in os.supports_fd6 trong C Ánh xạ này được ghi lại khi mô-đun được nhập lần đầu tiên, thường là trong quá trình khởi động Python như một phần của quá trình xử lý os.chdir in os.supports_fd8. Những thay đổi đối với môi trường được thực hiện sau thời gian này không được phản ánh trong , ngoại trừ những thay đổi được thực hiện bằng cách sửa đổi trực tiếp Ánh xạ này có thể được sử dụng để sửa đổi môi trường cũng như truy vấn môi trường. sẽ được gọi tự động khi ánh xạ được sửa đổi Trên Unix, khóa và giá trị sử dụng và trình xử lý lỗi os.stat in os.supports_follow_symlinks3. Use if you would like to use a different encoding Note Calling directly does not change , so it’s better to modify Note On some platforms, including FreeBSD and macOS, setting os.stat in os.supports_follow_symlinks8 may cause memory leaks. Refer to the system documentation for os.stat in os.supports_follow_symlinks1 You can delete items in this mapping to unset environment variables. will be called automatically when an item is deleted from , and when one of the import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories2 or import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories3 methods is called Đã thay đổi trong phiên bản 3. 9. Cập nhật để hỗ trợ các toán tử hợp nhất ( import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories4) và cập nhật ( import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories5) của PEP 584. os. môi trường Phiên bản byte của. một đối tượng trong đó cả khóa và giá trị đều là đối tượng đại diện cho môi trường quy trình. và được đồng bộ hóa (sửa đổi các bản cập nhật và ngược lại) chỉ khả dụng nếu là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 Mới trong phiên bản 3. 2 Đã thay đổi trong phiên bản 3. 9. Cập nhật để hỗ trợ các toán tử hợp nhất ( import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories4) và cập nhật ( import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories5) của PEP 584. os. chdir(đường dẫn)os. fchdir(fd)os. getcwd() Các chức năng này được mô tả trong os. mã fsencode(tên tệp)Mã hóa tên tệp thành ; là hàm ngược Mới trong phiên bản 3. 2 Đã thay đổi trong phiên bản 3. 6. Hỗ trợ được thêm vào để chấp nhận các đối tượng triển khai giao diện. os. fsdecode(tên tệp)Giải mã tên tệp từ ; là hàm ngược Mới trong phiên bản 3. 2 Đã thay đổi trong phiên bản 3. 6. Hỗ trợ được thêm vào để chấp nhận các đối tượng triển khai giao diện. os. fspath(đường dẫn)Trả về biểu diễn hệ thống tệp của đường dẫn Nếu hoặc được truyền vào, nó được trả về không thay đổi. Mặt khác được gọi và giá trị của nó được trả về miễn là nó là một đối tượng hoặc. Trong tất cả các trường hợp khác, được nâng lên Mới trong phiên bản 3. 6 lớp os. Giống đường dẫnMột đối tượng đại diện cho đường dẫn hệ thống tệp, e. g. Mới trong phiên bản 3. 6 phương thức trừu tượng __fspath__()Trả về biểu diễn đường dẫn hệ thống tệp của đối tượng Phương thức chỉ nên trả về một hoặc đối tượng, với ưu tiên là cho os. getenv(khóa , mặc định=None)Trả về giá trị của khóa biến môi trường dưới dạng chuỗi nếu tồn tại hoặc mặc định nếu không. khóa là một chuỗi. Lưu ý rằng vì sử dụng nên ánh xạ của cũng được ghi lại tương tự khi nhập và hàm có thể không phản ánh các thay đổi môi trường trong tương lai Trên Unix, các khóa và giá trị được giải mã bằng và trình xử lý lỗi os.stat in os.supports_follow_symlinks3. Sử dụng nếu bạn muốn sử dụng một mã hóa khác Hệ điều hành Unix, Windows os. getenvb(phím , mặc định=None)Trả về giá trị của khóa biến môi trường dưới dạng byte nếu nó tồn tại hoặc mặc định nếu nó không tồn tại. khóa phải là byte. Lưu ý rằng vì sử dụng nên ánh xạ của cũng được ghi lại tương tự khi nhập và hàm có thể không phản ánh các thay đổi môi trường trong tương lai chỉ khả dụng nếu là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 Unix Mới trong phiên bản 3. 2 os. get_exec_path(env=Không có)Trả về danh sách các thư mục sẽ được tìm kiếm cho một tệp thực thi được đặt tên, tương tự như trình bao, khi khởi chạy một quy trình. env, khi được chỉ định, phải là một từ điển biến môi trường để tra cứu PATH trong. Theo mặc định, khi env là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35, được sử dụng Mới trong phiên bản 3. 2 os. getegid()Return the effective group id of the current process. This corresponds to the “set id” bit on the file being executed in the current process Unix, không phải Emscripten, không phải WASI os. geteuid()Return the current process’s effective user id Unix, không phải Emscripten, không phải WASI os. getgid()Return the real group id of the current process Unix The function is a stub on Emscripten and WASI, see for more information os. getgrouplist(user , group , /)Return list of group ids that user belongs to. If group is not in the list, it is included; typically, group is specified as the group ID field from the password record for user, because that group ID will otherwise be potentially omitted Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. getgroups()Return list of supplemental group ids associated with the current process Unix, không phải Emscripten, không phải WASI Note On macOS, behavior differs somewhat from other Unix platforms. If the Python interpreter was built with a deployment target of if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"38 or earlier, returns the list of effective group ids associated with the current user process; this list is limited to a system-defined number of entries, typically 16, and may be modified by calls to if suitably privileged. If built with a deployment target greater than if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"38, returns the current group access list for the user associated with the effective user id of the process; the group access list may change over the lifetime of the process, it is not affected by calls to , and its length is not limited to 16. The deployment target value, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"44, can be obtained with os. getlogin() Return the name of the user logged in on the controlling terminal of the process. For most purposes, it is more useful to use since the latter checks the environment variables if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"47 or if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"48 to find out who the user is, and falls back to if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"49 to get the login name of the current real user id Unix, Windows, not Emscripten, not WASI os. getpgid(pid)Return the process group id of the process with process id pid. If pid is 0, the process group id of the current process is returned Unix, không phải Emscripten, không phải WASI os. getpgrp()Return the id of the current process group Unix, không phải Emscripten, không phải WASI os. getpid()Return the current process id The function is a stub on Emscripten and WASI, see for more information os. getppid()Return the parent’s process id. Khi quá trình mẹ đã thoát, trên Unix, id được trả về là một trong quá trình init (1), trên Windows, nó vẫn là cùng một id, có thể đã được sử dụng lại bởi một quá trình khác Unix, Windows, not Emscripten, not WASI Changed in version 3. 2. Added support for Windows. os. getpriority(which , who)Get program scheduling priority. The value which is one of , , or , and who is interpreted relative to which (a process identifier for , process group identifier for , and a user ID for ). A zero value for who denotes (respectively) the calling process, the process group of the calling process, or the real user ID of the calling process Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. PRIO_PROCESSos. PRIO_PGRPos. PRIO_USERParameters for the and functions Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. getresuid()Return a tuple (ruid, euid, suid) denoting the current process’s real, effective, and saved user ids Unix, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 2 os. getresgid()Return a tuple (rgid, egid, sgid) denoting the current process’s real, effective, and saved group ids Unix, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 2 os. getuid()Return the current process’s real user id Unix The function is a stub on Emscripten and WASI, see for more information os. initgroups(username , gid , /)Call the system initgroups() to initialize the group access list with all of the groups of which the specified username is a member, plus the specified group id Unix, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 2 os. putenv(key , value , /)Set the environment variable named key to the string value. Such changes to the environment affect subprocesses started with , or and Assignments to items in are automatically translated into corresponding calls to ; however, calls to don’t update , so it is actually preferable to assign to items of . Điều này cũng áp dụng cho and , tương ứng sử dụng và trong quá trình triển khai của chúng Note On some platforms, including FreeBSD and macOS, setting os.stat in os.supports_follow_symlinks8 may cause memory leaks. Refer to the system documentation for os.stat in os.supports_follow_symlinks1 Raises an if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"73 with arguments if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"74, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"75 Changed in version 3. 9. Chức năng này hiện luôn khả dụng. os. setegid(egid , /)Đặt id nhóm hiệu quả của quy trình hiện tại Unix, không phải Emscripten, không phải WASI os. seteuid(euid , /)Set the current process’s effective user id Unix, không phải Emscripten, không phải WASI os. setgid(gid , /)Set the current process’ group id Unix, không phải Emscripten, không phải WASI os. setgroups(groups , /)Set the list of supplemental group ids associated with the current process to groups. groups must be a sequence, and each element must be an integer identifying a group. This operation is typically available only to the superuser Unix, không phải Emscripten, không phải WASI Note On macOS, the length of groups may not exceed the system-defined maximum number of effective group ids, typically 16. See the documentation for for cases where it may not return the same group list set by calling setgroups() os. setpgrp()Call the system call if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"77 or if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"78 depending on which version is implemented (if any). See the Unix manual for the semantics Unix, không phải Emscripten, không phải WASI os. setpgid(pid , pgrp , /)Call the system call if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"79 to set the process group id of the process with id pid to the process group with id pgrp. See the Unix manual for the semantics Unix, không phải Emscripten, không phải WASI os. setpriority(which , who , priority)Set program scheduling priority. The value which is one of , , or , and who is interpreted relative to which (a process identifier for , process group identifier for , and a user ID for ). A zero value for who denotes (respectively) the calling process, the process group of the calling process, or the real user ID of the calling process. priority is a value in the range -20 to 19. The default priority is 0; lower priorities cause more favorable scheduling Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. setregid(rgid , egid, /)Set the current process’s real and effective group ids Unix, không phải Emscripten, không phải WASI os. setresgid(rgid , egid , sgid , /)Set the current process’s real, effective, and saved group ids Unix, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 2 os. setresuid(ruid , euid , suid , /)Set the current process’s real, effective, and saved user ids Unix, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 2 os. setreuid(ruid , euid , /)Set the current process’s real and effective user ids Unix, không phải Emscripten, không phải WASI os. getsid(pid , /)Call the system call if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"86. See the Unix manual for the semantics Unix, không phải Emscripten, không phải WASI os. setsid()Call the system call if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"87. See the Unix manual for the semantics Unix, không phải Emscripten, không phải WASI os. setuid(uid , /)Set the current process’s user id Unix, không phải Emscripten, không phải WASI os. strerror(code , /)Return the error message corresponding to the error code in code. On platforms where if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"88 returns if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"89 when given an unknown error number, is raisedos. supports_bytes_environ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if the native OS type of the environment is bytes (eg. if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 on Windows) Mới trong phiên bản 3. 2 os. umask(mask , /)Đặt ô số hiện tại và trả về ô trước đó The function is a stub on Emscripten and WASI, see for more information os. uname()Trả về thông tin xác định hệ điều hành hiện tại. The return value is an object with five attributes
For backwards compatibility, this object is also iterable, behaving like a five-tuple containing if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"93, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"94, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"95, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"96, and if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"97 in that order Some systems truncate if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"94 to 8 characters or to the leading component; a better way to get the hostname is or even try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()05 Unix Changed in version 3. 3. Return type changed from a tuple to a tuple-like object with named attributes. os. unsetenv(key , /)Unset (delete) the environment variable named key. Such changes to the environment affect subprocesses started with , or and Deletion of items in is automatically translated into a corresponding call to ; however, calls to don’t update , so it is actually preferable to delete items of Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()15 with argument if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"74 Changed in version 3. 9. The function is now always available and is also available on Windows. File Object CreationThese functions create new . (See also for opening file descriptors. ) os. fdopen(fd , *args , **kwargs)Return an open file object connected to the file descriptor fd. This is an alias of the built-in function and accepts the same arguments. The only difference is that the first argument of must always be an integer File Descriptor OperationsThese functions operate on I/O streams referenced using file descriptors File descriptors are small integers corresponding to a file that has been opened by the current process. For example, standard input is usually file descriptor 0, standard output is 1, and standard error is 2. Further files opened by a process will then be assigned 3, 4, 5, and so forth. The name “file descriptor” is slightly deceptive; on Unix platforms, sockets and pipes are also referenced by file descriptors The method can be used to obtain the file descriptor associated with a when required. Note that using the file descriptor directly will bypass the file object methods, ignoring aspects such as internal buffering of data os. close(fd)Close file descriptor fd Note This function is intended for low-level I/O and must be applied to a file descriptor as returned by or . To close a “file object” returned by the built-in function or by or , use its method os. closerange(fd_low , fd_high , /)Close all file descriptors from fd_low (inclusive) to fd_high (exclusive), ignoring errors. Equivalent to (but much faster than) for fd in range(fd_low, fd_high): try: os.close(fd) except OSError: passos. copy_file_range(src , dst , count , offset_src=None , offset_dst=None) Copy count bytes from file descriptor src, starting from offset offset_src, to file descriptor dst, starting from offset offset_dst. If offset_src is None, then src is read from the current position; respectively for offset_dst. Các tệp được trỏ bởi src và dst phải nằm trong cùng một hệ thống tệp, nếu không, một tệp được đặt thành This copy is done without the additional cost of transferring data from the kernel to user space and then back into the kernel. Additionally, some filesystems could implement extra optimizations. The copy is done as if both files are opened as binary The return value is the amount of bytes copied. This could be less than the amount requested Linux >= 4. 5 with glibc >= 2. 27 New in version 3. 8 os. device_encoding(fd)Return a string describing the encoding of the device associated with fd if it is connected to a terminal; else return On Unix, if the is enabled, return try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()31 rather than the device encoding Changed in version 3. 10. On Unix, the function now implements the Python UTF-8 Mode. os. dup(fd , /)Return a duplicate of file descriptor fd. The new file descriptor is On Windows, when duplicating a standard stream (0. stdin, 1. stdout, 2. stderr), the new file descriptor is not WASI Changed in version 3. 4. The new file descriptor is now non-inheritable. os. dup2(fd , fd2 , inheritable=True)Duplicate file descriptor fd to fd2, closing the latter first if necessary. Return fd2. The new file descriptor is by default or non-inheritable if inheritable is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 not WASI Đã thay đổi trong phiên bản 3. 4. Add the optional inheritable parameter. Changed in version 3. 7. Return fd2 on success. Previously, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35 was always returned. os. fchmod(fd , mode) Change the mode of the file given by fd to the numeric mode. See the docs for for possible values of mode. As of Python 3. 3, this is equivalent to try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()35 Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()36 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()38, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Unix The function is limited on Emscripten and WASI, see for more information os. fchown(fd , uid , gid)Change the owner and group id of the file given by fd to the numeric uid and gid. To leave one of the ids unchanged, set it to -1. See . As of Python 3. 3, this is equivalent to try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()41 Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()42 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()44, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()45, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Unix The function is limited on Emscripten and WASI, see for more information os. fdatasync(fd)Force write of file with filedescriptor fd to disk. Does not force update of metadata Unix Note This function is not available on MacOS os. fpathconf(fd , name , /)Return system configuration information relevant to an open file. name specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX. 1, Unix 95, Unix 98, and others). Some platforms define additional names as well. The names known to the host operating system are given in the try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()47 dictionary. For configuration variables not included in that mapping, passing an integer for name is also accepted If name is a string and is not known, is raised. If a specific value for name is not supported by the host system, even if it is included in try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()47, an is raised with for the error number As of Python 3. 3, điều này tương đương với try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()52 Unix os. fstat(fd)Get the status of the file descriptor fd. Return a object As of Python 3. 3, this is equivalent to try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()54 See also The function os. fstatvfs(fd , /)Return information about the filesystem containing the file associated with file descriptor fd, like . As of Python 3. 3, điều này tương đương với try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()57 Unix os. fsync(fd)Buộc ghi tệp với filedescriptor fd vào đĩa. On Unix, this calls the native try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()58 function; on Windows, the MS try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()59 function If you’re starting with a buffered Python f, first do try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()60, and then do try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()61, to ensure that all internal buffers associated with f are written to disk Hệ điều hành Unix, Windows os. ftruncate(fd , độ dài, /)Truncate the file corresponding to file descriptor fd, so that it is at most length bytes in size. As of Python 3. 3, this is equivalent to try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()62 Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()63 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()64, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()65 Hệ điều hành Unix, Windows Changed in version 3. 5. Added support for Windows os. get_blocking(fd , /)Get the blocking mode of the file descriptor. if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 if the flag is set, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if the flag is cleared See also and Unix The function is limited on Emscripten and WASI, see for more information New in version 3. 5 os. isatty(fd , /)Return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if the file descriptor fd is open and connected to a tty(-like) device, else if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92os. lockf(fd , cmd , len , /) Apply, test or remove a POSIX lock on an open file descriptor. fd is an open file descriptor. cmd specifies the command to use - one of , , or . len specifies the section of the file to lock Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()77 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()64, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()79, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()80 Unix New in version 3. 3 os. F_LOCKos. F_TLOCKos. F_ULOCKos. F_TESTFlags that specify what action will take Unix New in version 3. 3 os. login_tty(fd , /)Prepare the tty of which fd is a file descriptor for a new login session. Make the calling process a session leader; make the tty the controlling tty, the stdin, the stdout, and the stderr of the calling process; close fd Unix, không phải Emscripten, không phải WASI New in version 3. 11 os. lseek(fd , pos , how , /)Set the current position of file descriptor fd to position pos, modified by how. or try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()83 to set the position relative to the beginning of the file; or try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()85 to set it relative to the current position; or try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()87 to set it relative to the end of the file. Return the new cursor position in bytes, starting from the beginningos. SEEK_SETos. SEEK_CURos. SEEK_END Các tham số cho chức năng. Giá trị của chúng lần lượt là 0, 1 và 2 New in version 3. 3. Some operating systems could support additional values, like try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()89 or try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()90. os. open(path , flags , mode=0o777 , * , dir_fd=None) Open the file path and set various flags according to flags and possibly its mode according to mode. When computing mode, the current umask value is first masked out. Return the file descriptor for the newly opened file. The new file descriptor is For a description of the flag and mode values, see the C run-time documentation; flag constants (like and ) are defined in the module. Đặc biệt, trên Windows cần thêm để mở tệp ở chế độ nhị phân This function can support with the dir_fd parameter Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()95 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()38, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()98 Changed in version 3. 4. The new file descriptor is now non-inheritable. Note This function is intended for low-level I/O. For normal usage, use the built-in function , which returns a with with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)00 and with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)01 methods (and many more). To wrap a file descriptor in a file object, use New in version 3. 3. The dir_fd argument. Changed in version 3. 5. If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an exception (see PEP 475 for the rationale). Changed in version 3. 6. Accepts a . The following constants are options for the flags parameter to the function. They can be combined using the bitwise OR operator import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories4. Some of them are not available on all platforms. For descriptions of their availability and use, consult the open(2) manual page on Unix or the MSDN on Windowsos. O_RDONLYos. O_WRONLYos. O_RDWRos. O_APPENDos. O_CREATos. O_EXCLos. O_TRUNC The above constants are available on Unix and Windows os. O_DSYNCos. O_RSYNCos. O_SYNCos. O_NDELAYos. O_NONBLOCKos. O_NOCTTYos. O_CLOEXECThe above constants are only available on Unix Đã thay đổi trong phiên bản 3. 3. Add constant. os. O_BINARYos. O_NOINHERITos. O_SHORT_LIVEDos. O_TEMPORARYos. O_RANDOMos. O_SEQUENTIALos. O_TEXTThe above constants are only available on Windows os. O_EVTONLYos. O_FSYNCos. O_SYMLINKos. O_NOFOLLOW_ANYThe above constants are only available on macOS Changed in version 3. 10. Add , , and constants. os. O_ASYNCos. O_DIRECTos. O_DIRECTORYos. O_NOFOLLOWos. O_NOATIMEos. O_PATHos. O_TMPFILEos. O_SHLOCKos. O_EXLOCKThe above constants are extensions and not present if they are not defined by the C library Changed in version 3. 4. Add on systems that support it. Add , only available on Linux Kernel 3. 11 or newer. os. openpty()Open a new pseudo-terminal pair. Return a pair of file descriptors with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)13 for the pty and the tty, respectively. The new file descriptors are . For a (slightly) more portable approach, use the module Unix, không phải Emscripten, không phải WASI Changed in version 3. 4. The new file descriptors are now non-inheritable. os. pipe()Create a pipe. Return a pair of file descriptors with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)15 usable for reading and writing, respectively. The new file descriptor is Hệ điều hành Unix, Windows Changed in version 3. 4. The new file descriptors are now non-inheritable. os. pipe2(flags , /)Create a pipe with flags set atomically. flags can be constructed by ORing together one or more of these values. , . Return a pair of file descriptors with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)15 usable for reading and writing, respectively Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. posix_fallocate(fd , offset , len , /)Ensures that enough disk space is allocated for the file specified by fd starting from offset and continuing for len bytes Unix, not Emscripten New in version 3. 3 os. posix_fadvise(fd , offset , len , advice , /)Announces an intention to access data in a specific pattern thus allowing the kernel to make optimizations. The advice applies to the region of the file specified by fd starting at offset and continuing for len bytes. advice is one of , , , , or Unix New in version 3. 3 os. POSIX_FADV_NORMALos. POSIX_FADV_SEQUENTIALos. POSIX_FADV_RANDOMos. POSIX_FADV_NOREUSEos. POSIX_FADV_WILLNEEDos. POSIX_FADV_DONTNEEDFlags that can be used in advice in that specify the access pattern that is likely to be used Unix New in version 3. 3 os. pread(fd , n , offset , /)Read at most n bytes from file descriptor fd at a position of offset, leaving the file offset unchanged Return a bytestring containing the bytes read. If the end of the file referred to by fd has been reached, an empty bytes object is returned Unix New in version 3. 3 os. preadv(fd , buffers , offset , flags=0 , /)Read from a file descriptor fd at a position of offset into mutable buffers, leaving the file offset unchanged. Transfer data into each buffer until it is full and then move on to the next buffer in the sequence to hold the rest of the data The flags argument contains a bitwise OR of zero or more of the following flags Return the total number of bytes actually read which can be less than the total capacity of all the objects The operating system may set a limit ( value with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)29) on the number of buffers that can be used Combine the functionality of and Linux >= 2. 6. 30, FreeBSD >= 6. 0, OpenBSD >= 2. 7, AIX >= 7. 1 Using flags requires Linux >= 4. 6 Mới trong phiên bản 3. 7 os. RWF_NOWAITDo not wait for data which is not immediately available. If this flag is specified, the system call will return instantly if it would have to read data from the backing storage or wait for a lock If some data was successfully read, it will return the number of bytes read. If no bytes were read, it will return with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)32 and set errno to Linux >= 4. 14 Mới trong phiên bản 3. 7 Đọc/ghi ưu tiên cao. Cho phép các hệ thống tệp dựa trên khối sử dụng tính năng bỏ phiếu của thiết bị, mang lại độ trễ thấp hơn nhưng có thể sử dụng các tài nguyên bổ sung Hiện tại, trên Linux, tính năng này chỉ có thể sử dụng được trên bộ mô tả tệp được mở bằng cờ Linux >= 4. 6 Mới trong phiên bản 3. 7 os. pwrite(fd , str, offset, /)Viết chuỗi byte trong str vào bộ mô tả tệp fd tại vị trí của phần bù, giữ nguyên phần bù của tệp Trả về số byte thực sự được ghi Unix New in version 3. 3 os. pwritev(fd , bộ đệm, offset, flags=0, /)Ghi nội dung bộ đệm vào bộ mô tả tệp fd ở độ lệch offset, giữ nguyên độ lệch tệp. bộ đệm phải là một chuỗi các. Bộ đệm được xử lý theo thứ tự mảng. Toàn bộ nội dung của bộ đệm đầu tiên được ghi trước khi tiếp tục với bộ đệm thứ hai, v.v. The flags argument contains a bitwise OR of zero or more of the following flags Trả về tổng số byte thực sự được ghi The operating system may set a limit ( value with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)29) on the number of buffers that can be used Combine the functionality of and Linux >= 2. 6. 30, FreeBSD >= 6. 0, OpenBSD >= 2. 7, AIX >= 7. 1 Using flags requires Linux >= 4. 6 Mới trong phiên bản 3. 7 os. RWF_DSYNCCung cấp tương đương với mỗi lần ghi của cờ. Hiệu ứng cờ này chỉ áp dụng cho phạm vi dữ liệu được ghi bởi lệnh gọi hệ thống Linux >= 4. 7 Mới trong phiên bản 3. 7 os. RWF_SYNCCung cấp tương đương với mỗi lần ghi của cờ. Hiệu ứng cờ này chỉ áp dụng cho phạm vi dữ liệu được ghi bởi lệnh gọi hệ thống Linux >= 4. 7 Mới trong phiên bản 3. 7 os. RWF_APPENDProvide a per-write equivalent of the flag. This flag is meaningful only for , and its effect applies only to the data range written by the system call. The offset argument does not affect the write operation; the data is always appended to the end of the file. However, if the offset argument is with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)32, the current file offset is updated Linux >= 4. 16 New in version 3. 10 os. read(fd , n , /)Read at most n bytes from file descriptor fd Return a bytestring containing the bytes read. If the end of the file referred to by fd has been reached, an empty bytes object is returned Note This function is intended for low-level I/O and must be applied to a file descriptor as returned by or . To read a “file object” returned by the built-in function or by or , or , use its with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)00 or with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)57 methods Changed in version 3. 5. If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an exception (see PEP 475 for the rationale). os. sendfile(out_fd , in_fd , offset , count)os. sendfile(out_fd , in_fd , offset , count , headers=() , trailers=() , flags=0)Copy count bytes from file descriptor in_fd to file descriptor out_fd starting at offset. Return the number of bytes sent. When EOF is reached return try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()83 The first function notation is supported by all platforms that define On Linux, if offset is given as if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35, the bytes are read from the current position of in_fd and the position of in_fd is updated The second case may be used on macOS and FreeBSD where headers and trailers are arbitrary sequences of buffers that are written before and after the data from in_fd is written. It returns the same as the first case On macOS and FreeBSD, a value of try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()83 for count specifies to send until the end of in_fd is reached All platforms support sockets as out_fd file descriptor, and some platforms allow other types (e. g. regular file, pipe) as well Cross-platform applications should not use headers, trailers and flags arguments Unix, không phải Emscripten, không phải WASI Note For a higher-level wrapper of , see New in version 3. 3 Changed in version 3. 9. Parameters out and in was renamed to out_fd and in_fd. os. set_blocking(fd , blocking , /)Set the blocking mode of the specified file descriptor. Set the flag if blocking is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92, clear the flag otherwise See also and Unix The function is limited on Emscripten and WASI, see for more information New in version 3. 5 os. SF_NODISKIOos. SF_MNOWAITos. SF_SYNCParameters to the function, if the implementation supports them Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. SF_NOCACHEParameter to the function, if the implementation supports it. The data won’t be cached in the virtual memory and will be freed afterwards Unix, không phải Emscripten, không phải WASI New in version 3. 11 os. splice(src , dst , count , offset_src=None , offset_dst=None)Transfer count bytes from file descriptor src, starting from offset offset_src, to file descriptor dst, starting from offset offset_dst. At least one of the file descriptors must refer to a pipe. If offset_src is None, then src is read from the current position; respectively for offset_dst. The offset associated to the file descriptor that refers to a pipe must be if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35. The files pointed by src and dst must reside in the same filesystem, otherwise an is raised with set to This copy is done without the additional cost of transferring data from the kernel to user space and then back into the kernel. Additionally, some filesystems could implement extra optimizations. The copy is done as if both files are opened as binary Upon successful completion, returns the number of bytes spliced to or from the pipe. A return value of 0 means end of input. If src refers to a pipe, then this means that there was no data to transfer, and it would not make sense to block because there are no writers connected to the write end of the pipe Linux >= 2. 6. 17 with glibc >= 2. 5 New in version 3. 10 os. SPLICE_F_MOVEos. SPLICE_F_NONBLOCKos. SPLICE_F_MORENew in version 3. 10 os. readv(fd , buffers , /)Read from a file descriptor fd into a number of mutable buffers. Transfer data into each buffer until it is full and then move on to the next buffer in the sequence to hold the rest of the data Return the total number of bytes actually read which can be less than the total capacity of all the objects The operating system may set a limit ( value with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)29) on the number of buffers that can be used Unix New in version 3. 3 os. tcgetpgrp(fd , /)Return the process group associated with the terminal given by fd (an open file descriptor as returned by ) Unix, không phải WASI os. tcsetpgrp(fd , pg , /)Set the process group associated with the terminal given by fd (an open file descriptor as returned by ) to pg Unix, không phải WASI os. ttyname(fd , /)Return a string which specifies the terminal device associated with file descriptor fd. If fd is not associated with a terminal device, an exception is raised Unix os. viết(fd , str, /)Write the bytestring in str to file descriptor fd Trả về số byte thực sự được ghi Note This function is intended for low-level I/O and must be applied to a file descriptor as returned by or . To write a “file object” returned by the built-in function or by or , or or , use its with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)01 method Changed in version 3. 5. If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an exception (see PEP 475 for the rationale). os. writev(fd , buffers , /)Write the contents of buffers to file descriptor fd. buffers must be a sequence of . Buffers are processed in array order. Entire contents of the first buffer is written before proceeding to the second, and so on Returns the total number of bytes actually written The operating system may set a limit ( value with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)29) on the number of buffers that can be used Unix New in version 3. 3 Truy vấn kích thước của một thiết bị đầu cuốiNew in version 3. 3 os. get_terminal_size(fd=STDOUT_FILENO , /)Return the size of the terminal window as with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)90, tuple of type The optional argument try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()64 (default with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)93, or standard output) specifies which file descriptor should be queried If the file descriptor is not connected to a terminal, an is raised is the high-level function which should normally be used, with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)96 is the low-level implementation Hệ điều hành Unix, Windows class os. terminal_sizeA subclass of tuple, holding with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)90 of the terminal window sizecolumns Width of the terminal window in characters linesHeight of the terminal window in characters Inheritance of File DescriptorsNew in version 3. 4 A file descriptor has an “inheritable” flag which indicates if the file descriptor can be inherited by child processes. Since Python 3. 4, file descriptors created by Python are non-inheritable by default On UNIX, non-inheritable file descriptors are closed in child processes at the execution of a new program, other file descriptors are inherited On Windows, non-inheritable handles and file descriptors are closed in child processes, except for standard streams (file descriptors 0, 1 and 2. stdin, stdout and stderr), which are always inherited. Using functions, all inheritable handles and all inheritable file descriptors are inherited. Using the module, all file descriptors except standard streams are closed, and inheritable handles are only inherited if the close_fds parameter is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 On WebAssembly platforms try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()3 and try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()4, the file descriptor cannot be modifiedos. get_inheritable(fd , /) Get the “inheritable” flag of the specified file descriptor (a boolean) os. set_inheritable(fd , inheritable , /)Set the “inheritable” flag of the specified file descriptor os. get_handle_inheritable(handle , /)Get the “inheritable” flag of the specified handle (a boolean) Windows os. set_handle_inheritable(handle , inheritable , /)Set the “inheritable” flag of the specified handle Windows Files and DirectoriesOn some Unix platforms, many of these functions support one or more of these features
Use the real uid/gid to test for access to path. Note that most operations will use the effective uid/gid, therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to path. mode should be to test the existence of path, or it can be the inclusive OR of one or more of , , and to test permissions. Return if access is allowed, if not. See the Unix man page access(2) for more information This function can support specifying and If effective_ids is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04, will perform its access checks using the effective uid/gid instead of the real uid/gid. effective_ids may not be supported on your platform; you can check whether or not it is available using . If it is unavailable, using it will raise a Note Using to check if a user is authorized to e. g. open a file before actually doing so using creates a security hole, because the user might exploit the short time interval between checking and opening the file to manipulate it. It’s preferable to use techniques. For example if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data" is better written as try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read() Note I/O operations may fail even when indicates that they would succeed, particularly for operations on network filesystems which may have permissions semantics beyond the usual POSIX permission-bit model Changed in version 3. 3. Added the dir_fd, effective_ids, and follow_symlinks parameters. Changed in version 3. 6. Accepts a . os. F_OKos. R_OKos. W_OKos. X_OKCác giá trị cần chuyển dưới dạng tham số chế độ để kiểm tra sự tồn tại, khả năng đọc, khả năng ghi và khả năng thực thi của đường dẫn, tương ứng os. chdir(đường dẫn)Thay đổi thư mục làm việc hiện tại thành đường dẫn Chức năng này có thể hỗ trợ. Bộ mô tả phải tham chiếu đến một thư mục đã mở, không phải một tệp đang mở Hàm này có thể nâng cao và các lớp con như , và Đưa ra một >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26438 với đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37 Mới trong phiên bản 3. 3. Đã thêm hỗ trợ chỉ định đường dẫn dưới dạng bộ mô tả tệp trên một số nền tảng. Changed in version 3. 6. Accepts a . os. chflags(đường dẫn , cờ, *, follow_symlinks=True)Set the flags of path to the numeric flags. cờ có thể nhận kết hợp (theo bit OR) của các giá trị sau (như được định nghĩa trong mô-đun) Chức năng này có thể hỗ trợ Raises an >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26453 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()98 Unix, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 3. Đối số follow_symlinks. Changed in version 3. 6. Accepts a . os. chmod(path , mode , * , dir_fd=None , follow_symlinks=True)Change the mode of path to the numeric mode. mode may take one of the following values (as defined in the module) or bitwise ORed combinations of them This function can support , and Note Although Windows supports , you can only set the file’s read-only flag with it (via the >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26462 and >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26461 constants or a corresponding integer value). All other bits are ignored The function is limited on Emscripten and WASI, see for more information Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()36 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()38, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 New in version 3. 3. Added support for specifying path as an open file descriptor, and the dir_fd and follow_symlinks arguments. Changed in version 3. 6. Accepts a . os. chown(path , uid , gid , * , dir_fd=None , follow_symlinks=True)Change the owner and group id of path to the numeric uid and gid. To leave one of the ids unchanged, set it to -1 This function can support , and See for a higher-level function that accepts names in addition to numeric ids Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()42 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()44, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()45, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Unix The function is limited on Emscripten and WASI, see for more information New in version 3. 3. Added support for specifying path as an open file descriptor, and the dir_fd and follow_symlinks arguments. Changed in version 3. 6. Supports a . os. chroot(path)Change the root directory of the current process to path Unix, không phải Emscripten, không phải WASI Changed in version 3. 6. Accepts a . os. fchdir(fd)Change the current working directory to the directory represented by the file descriptor fd. The descriptor must refer to an opened directory, not an open file. As of Python 3. 3, this is equivalent to >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26489 Đưa ra một >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26438 với đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37 Unix os. getcwd()Return a string representing the current working directory os. getcwdb()Return a bytestring representing the current working directory Changed in version 3. 8. The function now uses the UTF-8 encoding on Windows, rather than the ANSI code page. see PEP 529 for the rationale. The function is no longer deprecated on Windows. os. lchflags(path , flags)Set the flags of path to the numeric flags, like , but do not follow symbolic links. As of Python 3. 3, this is equivalent to >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26493 Raises an >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26453 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()98 Unix, không phải Emscripten, không phải WASI Changed in version 3. 6. Accepts a . os. lchmod(path , mode)Change the mode of path to the numeric mode. If path is a symlink, this affects the symlink rather than the target. See the docs for for possible values of mode. As of Python 3. 3, this is equivalent to >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26498 Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()36 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()38, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Unix Changed in version 3. 6. Accepts a . os. lchown(path , uid , gid)Change the owner and group id of path to the numeric uid and gid. This function will not follow symbolic links. As of Python 3. 3, this is equivalent to os.stat in os.supports_dir_fd03 Raises an try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()42 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()44, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()45, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Unix Changed in version 3. 6. Accepts a . os. link(src , dst , * , src_dir_fd=None , dst_dir_fd=None , follow_symlinks=True)Create a hard link pointing to src named dst Hàm này có thể hỗ trợ chỉ định src_dir_fd và/hoặc dst_dir_fd để cung cấp và Raises an os.stat in os.supports_dir_fd09 with arguments os.stat in os.supports_dir_fd10, os.stat in os.supports_dir_fd11, os.stat in os.supports_dir_fd12, os.stat in os.supports_dir_fd13 Hệ điều hành Unix, Windows Changed in version 3. 2. Added Windows support. New in version 3. 3. Added the src_dir_fd, dst_dir_fd, and follow_symlinks arguments. Changed in version 3. 6. Accepts a for src and dst. os. listdir(path='. ')Return a list containing the names of the entries in the directory given by path. The list is in arbitrary order, and does not include the special entries os.stat in os.supports_dir_fd14 and os.stat in os.supports_dir_fd15 even if they are present in the directory. If a file is removed from or added to the directory during the call of this function, whether a name for that file be included is unspecified path may be a . If path is of type import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7 (directly or indirectly through the interface), the filenames returned will also be of type import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7; in all other circumstances, they will be of type if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"10 This function can also support ; the file descriptor must refer to a directory Raises an os.stat in os.supports_dir_fd20 with argument try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37 Note To encode if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"10 filenames to import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7, use See also The function returns directory entries along with file attribute information, giving better performance for many common use cases Changed in version 3. 2. The path parameter became optional. New in version 3. 3. Added support for specifying path as an open file descriptor. Changed in version 3. 6. Accepts a . os. lstat(path , * , dir_fd=None)Perform the equivalent of an os.stat in os.supports_dir_fd26 system call on the given path. Similar to , but does not follow symbolic links. Return a object On platforms that do not support symbolic links, this is an alias for As of Python 3. 3, this is equivalent to os.stat in os.supports_dir_fd30 Chức năng này cũng có thể hỗ trợ See also The function Changed in version 3. 2. Added support for Windows 6. 0 (Vista) liên kết tượng trưng. Changed in version 3. 3. Added the dir_fd parameter. Changed in version 3. 6. Accepts a . Changed in version 3. 8. On Windows, now opens reparse points that represent another path (name surrogates), including symbolic links and directory junctions. Other kinds of reparse points are resolved by the operating system as for . os. mkdir(đường dẫn , chế độ=0o777, *, dir_fd=None)Tạo một thư mục có tên đường dẫn với chế độ chế độ số If the directory already exists, is raised. If a parent directory in the path does not exist, is raised On some systems, mode is ignored. Where it is used, the current umask value is first masked out. If bits other than the last 9 (i. e. the last 3 digits of the octal representation of the mode) are set, their meaning is platform-dependent. On some platforms, they are ignored and you should call explicitly to set them Chức năng này cũng có thể hỗ trợ Cũng có thể tạo các thư mục tạm thời; Raises an os.stat in os.supports_dir_fd38 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()38, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 New in version 3. 3. The dir_fd argument. Changed in version 3. 6. Accepts a . os. makedirs(name , mode=0o777 , exist_ok=False)Recursive directory creation function. Like , but makes all intermediate-level directories needed to contain the leaf directory The mode parameter is passed to for creating the leaf directory; see for how it is interpreted. To set the file permission bits of any newly created parent directories you can set the umask before invoking . The file permission bits of existing parent directories are not changed If exist_ok is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 (the default), a is raised if the target directory already exists Note will become confused if the path elements to create include (eg. “. ” on UNIX systems) This function handles UNC paths correctly Raises an os.stat in os.supports_dir_fd38 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()38, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 New in version 3. 2. The exist_ok parameter. Changed in version 3. 4. 1. Before Python 3. 4. 1, if exist_ok was if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 and the directory existed, would still raise an error if mode did not match the mode of the existing directory. Since this behavior was impossible to implement safely, it was removed in Python 3. 4. 1. See bpo-21082. Changed in version 3. 6. Accepts a . Changed in version 3. 7. The mode argument no longer affects the file permission bits of newly created intermediate-level directories. os. mkfifo(path , mode=0o666 , * , dir_fd=None)Create a FIFO (a named pipe) named path with numeric mode mode. The current umask value is first masked out from the mode Chức năng này cũng có thể hỗ trợ FIFOs are pipes that can be accessed like regular files. FIFOs exist until they are deleted (for example with ). Generally, FIFOs are used as rendezvous between “client” and “server” type processes. the server opens the FIFO for reading, and the client opens it for writing. Note that doesn’t open the FIFO — it just creates the rendezvous point Unix, không phải Emscripten, không phải WASI New in version 3. 3. The dir_fd argument. Changed in version 3. 6. Accepts a . os. mknod(path , mode=0o600 , device=0 , * , dir_fd=None)Create a filesystem node (file, device special file or named pipe) named path. mode specifies both the permissions to use and the type of node to be created, being combined (bitwise OR) with one of os.stat in os.supports_dir_fd57, os.stat in os.supports_dir_fd58, os.stat in os.supports_dir_fd59, and os.stat in os.supports_dir_fd60 (those constants are available in ). For os.stat in os.supports_dir_fd58 and os.stat in os.supports_dir_fd59, device defines the newly created device special file (probably using ), otherwise it is ignored Chức năng này cũng có thể hỗ trợ Unix, không phải Emscripten, không phải WASI New in version 3. 3. The dir_fd argument. Changed in version 3. 6. Accepts a . os. major(device , /)Extract the device major number from a raw device number (usually the os.stat in os.supports_dir_fd65 or os.stat in os.supports_dir_fd66 field from >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26440)os. minor(device , /) Extract the device minor number from a raw device number (usually the os.stat in os.supports_dir_fd65 or os.stat in os.supports_dir_fd66 field from >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26440)os. makedev(major , minor , /) Compose a raw device number from the major and minor device numbers os. pathconf(path , name)Return system configuration information relevant to a named file. name specifies the configuration value to retrieve; it may be a string which is the name of a defined system value; these names are specified in a number of standards (POSIX. 1, Unix 95, Unix 98 và các loại khác). Some platforms define additional names as well. The names known to the host operating system are given in the try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()47 dictionary. For configuration variables not included in that mapping, passing an integer for name is also accepted If name is a string and is not known, is raised. If a specific value for name is not supported by the host system, even if it is included in try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()47, an is raised with for the error number Chức năng này có thể hỗ trợ Unix Changed in version 3. 6. Accepts a . os. pathconf_namesDictionary mapping names accepted by and to the integer values defined for those names by the host operating system. This can be used to determine the set of names known to the system Unix os. readlink(path , * , dir_fd=None)Return a string representing the path to which the symbolic link points. The result may be either an absolute or relative pathname; if it is relative, it may be converted to an absolute pathname using os.stat in os.supports_dir_fd78 If the path is a string object (directly or indirectly through a interface), the result will also be a string object, and the call may raise a UnicodeDecodeError. If the path is a bytes object (direct or indirectly), the result will be a bytes object Chức năng này cũng có thể hỗ trợ When trying to resolve a path that may contain links, use to properly handle recursion and platform differences Hệ điều hành Unix, Windows Changed in version 3. 2. Added support for Windows 6. 0 (Vista) liên kết tượng trưng. New in version 3. 3. The dir_fd argument. Changed in version 3. 6. Accepts a on Unix. Changed in version 3. 8. Accepts a and a bytes object on Windows. Changed in version 3. 8. Added support for directory junctions, and changed to return the substitution path (which typically includes os.stat in os.supports_dir_fd81 prefix) rather than the optional “print name” field that was previously returned. os. remove(path , * , dir_fd=None) Remove (delete) the file path. If path is a directory, an is raised. Use to remove directories. If the file does not exist, a is raised Chức năng này có thể hỗ trợ On Windows, attempting to remove a file that is in use causes an exception to be raised; on Unix, the directory entry is removed but the storage allocated to the file is not made available until the original file is no longer in use This function is semantically identical to Raises an os.stat in os.supports_dir_fd86 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 New in version 3. 3. The dir_fd argument. Changed in version 3. 6. Accepts a . os. removedirs(name)Remove directories recursively. Works like except that, if the leaf directory is successfully removed, tries to successively remove every parent directory mentioned in path until an error is raised (which is ignored, because it generally means that a parent directory is not empty). For example, os.stat in os.supports_dir_fd91 will first remove the directory os.stat in os.supports_dir_fd92, and then remove os.stat in os.supports_dir_fd93 and os.stat in os.supports_dir_fd94 if they are empty. Raises if the leaf directory could not be successfully removed Raises an os.stat in os.supports_dir_fd86 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Changed in version 3. 6. Accepts a . os. rename(src , dst , * , src_dir_fd=None , dst_dir_fd=None)Đổi tên tệp hoặc thư mục src thành dst. If dst exists, the operation will fail with an subclass in a number of cases On Windows, if dst exists a is always raised. The operation may fail if src and dst are on different filesystems. Use to support moves to a different filesystem On Unix, if src is a file and dst is a directory or vice-versa, an or a will be raised respectively. If both are directories and dst is empty, dst will be silently replaced. If dst is a non-empty directory, an is raised. If both are files, dst will be replaced silently if the user has permission. Thao tác có thể không thành công trên một số hương vị Unix nếu src và dst nằm trên các hệ thống tệp khác nhau. If successful, the renaming will be an atomic operation (this is a POSIX requirement) This function can support specifying src_dir_fd and/or dst_dir_fd to supply If you want cross-platform overwriting of the destination, use Raises an os.access in os.supports_effective_ids06 with arguments os.stat in os.supports_dir_fd10, os.stat in os.supports_dir_fd11, os.stat in os.supports_dir_fd12, os.stat in os.supports_dir_fd13 New in version 3. 3. The src_dir_fd and dst_dir_fd arguments. Changed in version 3. 6. Accepts a for src and dst. Thư mục đệ quy hoặc chức năng đổi tên tập tin. Works like , except creation of any intermediate directories needed to make the new pathname good is attempted first. After the rename, directories corresponding to rightmost path segments of the old name will be pruned away using Note This function can fail with the new directory structure made if you lack permissions needed to remove the leaf directory or file Raises an os.access in os.supports_effective_ids06 with arguments os.stat in os.supports_dir_fd10, os.stat in os.supports_dir_fd11, os.stat in os.supports_dir_fd12, os.stat in os.supports_dir_fd13 Changed in version 3. 6. Accepts a for old and new. os. replace(src , dst , * , src_dir_fd=None , dst_dir_fd=None)Rename the file or directory src to dst. If dst is a non-empty directory, will be raised. If dst exists and is a file, it will be replaced silently if the user has permission. The operation may fail if src and dst are on different filesystems. If successful, the renaming will be an atomic operation (this is a POSIX requirement) This function can support specifying src_dir_fd and/or dst_dir_fd to supply Raises an os.access in os.supports_effective_ids06 with arguments os.stat in os.supports_dir_fd10, os.stat in os.supports_dir_fd11, os.stat in os.supports_dir_fd12, os.stat in os.supports_dir_fd13 New in version 3. 3 Changed in version 3. 6. Accepts a for src and dst. os. rmdir(path , * , dir_fd=None)Remove (delete) the directory path. If the directory does not exist or is not empty, a or an is raised respectively. In order to remove whole directory trees, can be used Chức năng này có thể hỗ trợ Raises an os.access in os.supports_effective_ids27 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 New in version 3. 3. The dir_fd parameter. Changed in version 3. 6. Accepts a . os. scandir(path='. ')Return an iterator of objects corresponding to the entries in the directory given by path. The entries are yielded in arbitrary order, and the special entries os.stat in os.supports_dir_fd14 and os.stat in os.supports_dir_fd15 are not included. If a file is removed from or added to the directory after creating the iterator, whether an entry for that file be included is unspecified Using instead of can significantly increase the performance of code that also needs file type or file attribute information, because objects expose this information if the operating system provides it when scanning a directory. All methods may perform a system call, but and usually only require a system call for symbolic links; always requires a system call on Unix but only requires one for symbolic links on Windows path may be a . If path is of type import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7 (directly or indirectly through the interface), the type of the and attributes of each will be import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7; in all other circumstances, they will be of type if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"10 This function can also support ; the file descriptor must refer to a directory Raises an os.access in os.supports_effective_ids47 with argument try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37 The iterator supports the protocol and has the following method scandir. close()Close the iterator and free acquired resources This is called automatically when the iterator is exhausted or garbage collected, or when an error happens during iterating. However it is advisable to call it explicitly or use the statement Mới trong phiên bản 3. 6 The following example shows a simple use of to display all the files (excluding directories) in the given path that don’t start with os.stat in os.supports_dir_fd14. The os.access in os.supports_effective_ids53 call will generally not make an additional system call with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name) Note On Unix-based systems, uses the system’s opendir() and readdir() functions. On Windows, it uses the Win32 FindFirstFileW and FindNextFileW functions New in version 3. 5 New in version 3. 6. Added support for the protocol and the method. If a iterator is neither exhausted nor explicitly closed a will be emitted in its destructor. The function accepts a Changed in version 3. 7. Added support for on Unix. class os. DirEntryObject yielded by to expose the file path and other file attributes of a directory entry will provide as much of this information as possible without making additional system calls. When a try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()55 or os.stat in os.supports_dir_fd26 system call is made, the os.access in os.supports_effective_ids30 object will cache the result os.access in os.supports_effective_ids30 instances are not intended to be stored in long-lived data structures; if you know the file metadata has changed or if a long time has elapsed since calling , call os.access in os.supports_effective_ids65 to fetch up-to-date information Because the os.access in os.supports_effective_ids30 methods can make operating system calls, they may also raise . If you need very fine-grained control over errors, you can catch when calling one of the os.access in os.supports_effective_ids30 methods and handle as appropriate To be directly usable as a , os.access in os.supports_effective_ids30 implements the interface Các thuộc tính và phương thức trên một phiên bản os.access in os.supports_effective_ids30 như sauname The entry’s base filename, relative to the path argument The attribute will be import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7 if the path argument is of type import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7 and if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"10 otherwise. Use to decode byte filenamespath The entry’s full path name. equivalent to os.access in os.supports_effective_ids80 where scandir_path is the path argument. The path is only absolute if the path argument was absolute. If the path argument was a , the attribute is the same as the attribute The attribute will be import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7 if the path argument is of type import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories7 and if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"10 otherwise. Use to decode byte filenamesinode() Return the inode number of the entry The result is cached on the os.access in os.supports_effective_ids30 object. Use os.access in os.supports_effective_ids93 to fetch up-to-date information On the first, uncached call, a system call is required on Windows but not on Unix is_dir(* , follow_symlinks=True)Return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if this entry is a directory or a symbolic link pointing to a directory; return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 if the entry is or points to any other kind of file, or if it doesn’t exist anymore If follow_symlinks is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92, return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 only if this entry is a directory (without following symlinks); return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 if the entry is any other kind of file or if it doesn’t exist anymore The result is cached on the os.access in os.supports_effective_ids30 object, with a separate cache for follow_symlinks if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 and if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92. Call along with to fetch up-to-date information On the first, uncached call, no system call is required in most cases. Specifically, for non-symlinks, neither Windows or Unix require a system call, except on certain Unix file systems, such as network file systems, that return os.chdir in os.supports_fd04. If the entry is a symlink, a system call will be required to follow the symlink unless follow_symlinks is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 This method can raise , such as , but is caught and not raised is_file(* , follow_symlinks=True)Return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if this entry is a file or a symbolic link pointing to a file; return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 if the entry is or points to a directory or other non-file entry, or if it doesn’t exist anymore If follow_symlinks is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92, return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 only if this entry is a file (without following symlinks); return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 if the entry is a directory or other non-file entry, or if it doesn’t exist anymore The result is cached on the os.access in os.supports_effective_ids30 object. Caching, system calls made, and exceptions raised are as per is_symlink() Return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if this entry is a symbolic link (even if broken); return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 if the entry points to a directory or any kind of file, or if it doesn’t exist anymore The result is cached on the os.access in os.supports_effective_ids30 object. Call to fetch up-to-date information On the first, uncached call, no system call is required in most cases. Specifically, neither Windows or Unix require a system call, except on certain Unix file systems, such as network file systems, that return os.chdir in os.supports_fd04 This method can raise , such as , but is caught and not raised stat(* , follow_symlinks=True)Return a object for this entry. This method follows symbolic links by default; to stat a symbolic link add the os.chdir in os.supports_fd25 argument Trên Unix, phương pháp này luôn yêu cầu một cuộc gọi hệ thống. On Windows, it only requires a system call if follow_symlinks is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 and the entry is a reparse point (for example, a symbolic link or directory junction) On Windows, the os.chdir in os.supports_fd27, os.stat in os.supports_dir_fd65 and os.chdir in os.supports_fd29 attributes of the are always set to zero. Call to get these attributes The result is cached on the os.access in os.supports_effective_ids30 object, with a separate cache for follow_symlinks if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 and if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92. Call to fetch up-to-date information Note that there is a nice correspondence between several attributes and methods of os.access in os.supports_effective_ids30 and of . In particular, the os.access in os.supports_effective_ids42 attribute has the same meaning, as do the os.access in os.supports_effective_ids37, os.access in os.supports_effective_ids38, os.chdir in os.supports_fd41 and try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()55 methods New in version 3. 5 Changed in version 3. 6. Added support for the interface. Added support for paths on Windows. os. chỉ số(đường dẫn , *, dir_fd=None, follow_symlinks=True)Get the status of a file or a file descriptor. Perform the equivalent of a try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()55 system call on the given path. path may be specified as either a string or bytes – directly or indirectly through the interface – or as an open file descriptor. Return a object This function normally follows symlinks; to stat a symlink add the argument os.chdir in os.supports_fd25, or use This function can support and On Windows, passing os.chdir in os.supports_fd25 will disable following all name-surrogate reparse points, which includes symlinks and directory junctions. Other types of reparse points that do not resemble links or that the operating system is unable to follow will be opened directly. When following a chain of multiple links, this may result in the original link being returned instead of the non-link that prevented full traversal. Để có được kết quả thống kê cho đường dẫn cuối cùng trong trường hợp này, hãy sử dụng chức năng để giải quyết tên đường dẫn càng nhiều càng tốt và gọi kết quả. This does not apply to dangling symlinks or junction points, which will raise the usual exceptions Example >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 264 See also and functions New in version 3. 3. Added the dir_fd and follow_symlinks arguments, specifying a file descriptor instead of a path. Changed in version 3. 6. Accepts a . Changed in version 3. 8. On Windows, all reparse points that can be resolved by the operating system are now followed, and passing os.chdir in os.supports_fd25 disables following all name surrogate reparse points. If the operating system reaches a reparse point that it is not able to follow, stat now returns the information for the original path as if os.chdir in os.supports_fd25 had been specified instead of raising an error. class os. stat_result Object whose attributes correspond roughly to the members of the >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26440 structure. It is used for the result of , and Attributes st_modeFile mode. file type and file mode bits (permissions) st_inoPlatform dependent, but if non-zero, uniquely identifies the file for a given value of os.stat in os.supports_dir_fd65. Typically
Identifier of the device on which this file resides st_nlinkNumber of hard links st_uidUser identifier of the file owner st_gidGroup identifier of the file owner st_sizeSize of the file in bytes, if it is a regular file or a symbolic link. The size of a symbolic link is the length of the pathname it contains, without a terminating null byte Timestamps st_atimeTime of most recent access expressed in seconds st_mtimeTime of most recent content modification expressed in seconds st_ctimePlatform dependent
Time of most recent access expressed in nanoseconds as an integer st_mtime_nsTime of most recent content modification expressed in nanoseconds as an integer st_ctime_nsPlatform dependent
Note The exact meaning and resolution of the , , and attributes depend on the operating system and the file system. For example, on Windows systems using the FAT or FAT32 file systems, has 2-second resolution, and has only 1-day resolution. See your operating system documentation for details Similarly, although , , and are always expressed in nanoseconds, many systems do not provide nanosecond precision. On systems that do provide nanosecond precision, the floating-point object used to store , , and cannot preserve all of it, and as such will be slightly inexact. If you need the exact timestamps you should always use , , and On some Unix systems (such as Linux), the following attributes may also be available st_blocksNumber of 512-byte blocks allocated for file. This may be smaller than /512 when the file has holes st_blksizeKích thước khối "ưa thích" cho I/O hệ thống tệp hiệu quả. Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite st_rdevType of device if an inode device st_flagsUser defined flags for file On other Unix systems (such as FreeBSD), the following attributes may be available (but may be only filled out if root tries to use them) st_genFile generation number st_birthtimeTime of file creation On Solaris and derivatives, the following attributes may also be available st_fstypeString that uniquely identifies the type of the filesystem that contains the file On macOS systems, the following attributes may also be available st_rsizeReal size of the file st_creatorCreator of the file st_typeFile type On Windows systems, the following attributes are also available st_file_attributesWindows file attributes. os.chdir in os.supports_fd77 member of the os.chdir in os.supports_fd78 structure returned by os.chdir in os.supports_fd79. See the os.chdir in os.supports_fd80 constants in the modulest_reparse_tag When has the os.chdir in os.supports_fd83 set, this field contains the tag identifying the type of reparse point. See the os.chdir in os.supports_fd84 constants in the module The standard module defines functions and constants that are useful for extracting information from a >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26440 structure. (On Windows, some items are filled with dummy values. ) For backward compatibility, a instance is also accessible as a tuple of at least 10 integers giving the most important (and portable) members of the >>> import os >>> statinfo = os.stat('somefile.txt') >>> statinfo os.stat_result(st_mode=33188, st_ino=7876932, st_dev=234881026, st_nlink=1, st_uid=501, st_gid=501, st_size=264, st_atime=1297230295, st_mtime=1297230027, st_ctime=1297230027) >>> statinfo.st_size 26440 structure, in the order , , , , , , , , , . More items may be added at the end by some implementations. For compatibility with older Python versions, accessing as a tuple always returns integers New in version 3. 3. Added the , , and members. Mới trong phiên bản 3. 5. Added the member on Windows. Changed in version 3. 5. Windows now returns the file index as when available. New in version 3. 7. Added the member to Solaris/derivatives. New in version 3. 8. Added the member on Windows. Đã thay đổi trong phiên bản 3. 8. On Windows, the member now identifies special files as os.stat in os.supports_follow_symlinks09, os.stat in os.supports_follow_symlinks10 or os.stat in os.supports_follow_symlinks11 as appropriate. os. statvfs(path) Thực hiện cuộc gọi hệ thống try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()56 trên đường dẫn đã cho. The return value is an object whose attributes describe the filesystem on the given path, and correspond to the members of the os.stat in os.supports_follow_symlinks13 structure, namely. os.stat in os.supports_follow_symlinks14, os.stat in os.supports_follow_symlinks15, os.stat in os.supports_follow_symlinks16, os.stat in os.supports_follow_symlinks17, os.stat in os.supports_follow_symlinks18, os.stat in os.supports_follow_symlinks19, os.stat in os.supports_follow_symlinks20, os.stat in os.supports_follow_symlinks21, os.stat in os.supports_follow_symlinks22, os.stat in os.supports_follow_symlinks23, os.stat in os.supports_follow_symlinks24 Two module-level constants are defined for the os.stat in os.supports_follow_symlinks22 attribute’s bit-flags. if os.stat in os.supports_follow_symlinks26 is set, the filesystem is mounted read-only, and if os.stat in os.supports_follow_symlinks27 is set, the semantics of setuid/setgid bits are disabled or not supported Additional module-level constants are defined for GNU/glibc based systems. These are os.stat in os.supports_follow_symlinks28 (disallow access to device special files), os.stat in os.supports_follow_symlinks29 (disallow program execution), os.stat in os.supports_follow_symlinks30 (writes are synced at once), os.stat in os.supports_follow_symlinks31 (allow mandatory locks on an FS), os.stat in os.supports_follow_symlinks32 (write on file/directory/symlink), os.stat in os.supports_follow_symlinks33 (append-only file), os.stat in os.supports_follow_symlinks34 (immutable file), os.stat in os.supports_follow_symlinks35 (do not update access times), os.stat in os.supports_follow_symlinks36 (do not update directory access times), os.stat in os.supports_follow_symlinks37 (update atime relative to mtime/ctime) Chức năng này có thể hỗ trợ Unix Changed in version 3. 2. Các hằng số os.stat in os.supports_follow_symlinks26 và os.stat in os.supports_follow_symlinks27 đã được thêm vào. New in version 3. 3. Added support for specifying path as an open file descriptor. Changed in version 3. 4. The os.stat in os.supports_follow_symlinks28, os.stat in os.supports_follow_symlinks29, os.stat in os.supports_follow_symlinks30, os.stat in os.supports_follow_symlinks31, os.stat in os.supports_follow_symlinks32, os.stat in os.supports_follow_symlinks33, os.stat in os.supports_follow_symlinks34, os.stat in os.supports_follow_symlinks35, os.stat in os.supports_follow_symlinks36, and os.stat in os.supports_follow_symlinks37 constants were added. Changed in version 3. 6. Accepts a . New in version 3. 7. Added os.stat in os.supports_follow_symlinks24. os. supports_dir_fd A object indicating which functions in the module accept an open file descriptor for their dir_fd parameter. Different platforms provide different features, and the underlying functionality Python uses to implement the dir_fd parameter is not available on all platforms Python supports. For consistency’s sake, functions that may support dir_fd always allow specifying the parameter, but will throw an exception if the functionality is used when it’s not locally available. (Specifying if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35 for dir_fd is always supported on all platforms. ) To check whether a particular function accepts an open file descriptor for its dir_fd parameter, use the os.stat in os.supports_follow_symlinks54 operator on os.stat in os.supports_follow_symlinks55. As an example, this expression evaluates to if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if accepts open file descriptors for dir_fd on the local platform os.stat in os.supports_dir_fd Currently dir_fd parameters only work on Unix platforms; none of them work on Windows New in version 3. 3 os. supports_effective_idsA object indicating whether permits specifying if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 for its effective_ids parameter on the local platform. (Specifying if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 for effective_ids is always supported on all platforms. ) If the local platform supports it, the collection will contain ; otherwise it will be empty This expression evaluates to if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if supports os.stat in os.supports_follow_symlinks65 on the local platform os.access in os.supports_effective_ids Currently effective_ids is only supported on Unix platforms; it does not work on Windows New in version 3. 3 os. supports_fdA object indicating which functions in the module permit specifying their path parameter as an open file descriptor on the local platform. Different platforms provide different features, and the underlying functionality Python uses to accept open file descriptors as path arguments is not available on all platforms Python supports To determine whether a particular function permits specifying an open file descriptor for its path parameter, use the os.stat in os.supports_follow_symlinks54 operator on os.stat in os.supports_follow_symlinks69. As an example, this expression evaluates to if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if accepts open file descriptors for path on your local platform os.chdir in os.supports_fd New in version 3. 3 os. supports_follow_symlinksMột đối tượng cho biết chức năng nào trong mô-đun chấp nhận if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 cho tham số follow_symlinks của chúng trên nền tảng cục bộ. Các nền tảng khác nhau cung cấp các tính năng khác nhau và chức năng cơ bản mà Python sử dụng để triển khai follow_symlinks không khả dụng trên tất cả các nền tảng mà Python hỗ trợ. Để đảm bảo tính nhất quán, các hàm có thể hỗ trợ follow_symlinks luôn cho phép chỉ định tham số, nhưng sẽ đưa ra một ngoại lệ nếu chức năng được sử dụng khi nó không khả dụng cục bộ. (Chỉ định if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 cho follow_symlinks luôn được hỗ trợ trên tất cả các nền tảng. ) Để kiểm tra xem một chức năng cụ thể có chấp nhận if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 cho tham số follow_symlinks của nó hay không, hãy sử dụng toán tử os.stat in os.supports_follow_symlinks54 trên os.stat in os.supports_follow_symlinks78. Ví dụ: biểu thức này ước tính thành if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 nếu bạn có thể chỉ định os.chdir in os.supports_fd25 khi gọi trên nền tảng địa phương os.stat in os.supports_follow_symlinks New in version 3. 3 os. liên kết tượng trưng(src , dst, target_is_directory=False, *, dir_fd=None)Tạo một liên kết tượng trưng trỏ đến src có tên là dst Trên Windows, một liên kết tượng trưng đại diện cho một tệp hoặc một thư mục và không biến thành mục tiêu một cách linh hoạt. Nếu có mục tiêu, loại liên kết tượng trưng sẽ được tạo để khớp. Otherwise, the symlink will be created as a directory if target_is_directory is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 or a file symlink (the default) otherwise. Trên các nền tảng không phải Windows, target_is_directory bị bỏ qua Chức năng này có thể hỗ trợ Note Trên các phiên bản Windows 10 mới hơn, các tài khoản không có đặc quyền có thể tạo liên kết tượng trưng nếu Chế độ nhà phát triển được bật. Khi Chế độ nhà phát triển không khả dụng/được bật, thì cần phải có đặc quyền SeCreateSymbolicLinkPrivilege hoặc quy trình phải được chạy với tư cách quản trị viên được nâng lên khi chức năng được gọi bởi người dùng không có đặc quyền Đưa ra một os.stat in os.supports_follow_symlinks84 với các đối số os.stat in os.supports_dir_fd10, os.stat in os.supports_dir_fd11, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Hệ điều hành Unix, Windows The function is limited on Emscripten and WASI, see for more information Changed in version 3. 2. Added support for Windows 6. 0 (Vista) liên kết tượng trưng. Mới trong phiên bản 3. 3. Đã thêm đối số dir_fd và hiện cho phép target_is_directory trên các nền tảng không phải Windows. Changed in version 3. 6. Accepts a for src and dst. Đã thay đổi trong phiên bản 3. 8. Đã thêm hỗ trợ cho các liên kết tượng trưng không được nâng cao trên Windows với Chế độ nhà phát triển. os. đồng bộ hóa()Buộc ghi mọi thứ vào đĩa Unix New in version 3. 3 os. truncate(path , length)Cắt bớt tệp tương ứng với đường dẫn, sao cho nó có kích thước tối đa là byte Chức năng này có thể hỗ trợ Tăng một try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()63 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()65 Hệ điều hành Unix, Windows New in version 3. 3 Changed in version 3. 5. Added support for Windows Changed in version 3. 6. Accepts a . os. hủy liên kết(đường dẫn , *, dir_fd=None)Xóa (xóa) đường dẫn tệp. Chức năng này giống hệt về mặt ngữ nghĩa với ; . Vui lòng xem tài liệu để biết thêm thông tin Raises an os.stat in os.supports_dir_fd86 with arguments try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 New in version 3. 3. The dir_fd parameter. Changed in version 3. 6. Accepts a . os. utime(đường dẫn , lần, *, [ns, ]dir_fd=None, follow_symlinks=True)Đặt thời gian truy cập và sửa đổi của tệp được chỉ định bởi đường dẫn có hai tham số tùy chọn, lần và ns. Chúng chỉ định thời gian được đặt trên đường dẫn và được sử dụng như sau
It is an error to specify tuples for both times and ns Lưu ý rằng thời gian chính xác mà bạn đặt ở đây có thể không được trả lại bằng lệnh gọi tiếp theo, tùy thuộc vào độ phân giải mà hệ điều hành của bạn ghi lại thời gian truy cập và sửa đổi; . Cách tốt nhất để duy trì thời gian chính xác là sử dụng các trường st_atime_ns và st_mtime_ns từ đối tượng kết quả với tham số ns để This function can support , and Đưa ra một import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories07 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories09, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories10, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Mới trong phiên bản 3. 3. Đã thêm hỗ trợ để chỉ định đường dẫn dưới dạng bộ mô tả tệp đang mở và các tham số dir_fd, follow_symlinks và ns. Changed in version 3. 6. Accepts a . os. đi bộ(top , topdown=True, onerror=None, followlinks=False)Tạo tên tệp trong cây thư mục bằng cách di chuyển cây từ trên xuống hoặc từ dưới lên. Đối với mỗi thư mục trong cây bắt nguồn từ đầu thư mục (bao gồm cả đầu), nó tạo ra 3-tuple import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories12 dirpath là một chuỗi, đường dẫn đến thư mục. dirnames là danh sách tên của các thư mục con trong dirpath (bao gồm các liên kết tượng trưng đến các thư mục và không bao gồm os.stat in os.supports_dir_fd14 và os.stat in os.supports_dir_fd15). tên tệp là danh sách tên của các tệp không phải thư mục trong dirpath. Lưu ý rằng tên trong danh sách không chứa thành phần đường dẫn. Để có đường dẫn đầy đủ (bắt đầu bằng top) tới một tệp hoặc thư mục trong dirpath, hãy thực hiện _ import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories15. Danh sách có được sắp xếp hay không tùy thuộc vào hệ thống tệp. Nếu một tệp bị xóa khỏi hoặc được thêm vào thư mục dirpath trong khi tạo danh sách, liệu tên của tệp đó có được đưa vào hay không sẽ không được chỉ định If optional argument topdown is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top-down). Nếu từ trên xuống là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92, bộ ba cho một thư mục được tạo sau bộ ba cho tất cả các thư mục con của nó (các thư mục được tạo từ dưới lên). Bất kể giá trị của topdown là gì, danh sách các thư mục con được truy xuất trước khi các bộ dữ liệu cho thư mục và các thư mục con của nó được tạo Khi topdown là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04, người gọi có thể sửa đổi danh sách dirnames tại chỗ (có thể sử dụng hoặc gán lát cắt) và sẽ chỉ chuyển tiếp vào các thư mục con có tên vẫn còn trong dirnames; . Sửa đổi dirnames khi topdown là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 không ảnh hưởng đến hành vi của đi bộ, bởi vì ở chế độ từ dưới lên, các thư mục trong dirnames được tạo trước khi chính dirpath được tạo Theo mặc định, các lỗi từ cuộc gọi sẽ bị bỏ qua. Nếu onerror đối số tùy chọn được chỉ định, nó phải là một hàm; . Nó có thể báo lỗi để tiếp tục đi bộ hoặc đưa ra ngoại lệ để hủy bỏ đi bộ. Lưu ý rằng tên tệp có sẵn dưới dạng thuộc tính import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories26 của đối tượng ngoại lệ Theo mặc định, sẽ không đi xuống các liên kết tượng trưng giải quyết các thư mục. Set followlinks to if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 to visit directories pointed to by symlinks, on systems that support them Note Xin lưu ý rằng việc đặt các liên kết theo dõi thành if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 có thể dẫn đến đệ quy vô hạn nếu một liên kết trỏ đến thư mục mẹ của chính nó. không theo dõi các thư mục mà nó đã truy cập Note Nếu bạn chuyển một tên đường dẫn tương đối, đừng thay đổi thư mục làm việc hiện tại giữa các lần nối lại. không bao giờ thay đổi thư mục hiện tại và giả định rằng người gọi nó cũng không Ví dụ này hiển thị số byte được lấy bởi các tệp không phải thư mục trong mỗi thư mục bên dưới thư mục bắt đầu, ngoại trừ việc nó không tìm trong bất kỳ thư mục con CVS nào import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories Trong ví dụ tiếp theo (triển khai đơn giản của ), đi bộ từ dưới lên trên cây là cần thiết, không cho phép xóa thư mục trước khi thư mục trống if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"0 Nâng cao một import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories35 với các đối số import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories36, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories37, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories38, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories39 Đã thay đổi trong phiên bản 3. 5. This function now calls instead of , making it faster by reducing the number of calls to . Changed in version 3. 6. Accepts a . os. đi bộ(top='. ' , topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)Điều này hoạt động chính xác như , ngoại trừ việc nó tạo ra 4-tuple import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories44 và nó hỗ trợ try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 dirpath, dirnames và tên tệp giống hệt với đầu ra và dirfd là một bộ mô tả tệp đề cập đến thư mục dirpath Chức năng này luôn hỗ trợ và. Tuy nhiên, xin lưu ý rằng, không giống như các chức năng khác, giá trị mặc định cho follow_symlinks là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 Note Vì mang lại các bộ mô tả tệp, chúng chỉ hợp lệ cho đến bước lặp tiếp theo, vì vậy bạn nên sao chép chúng (e. g. with ) nếu bạn muốn giữ chúng lâu hơn Ví dụ này hiển thị số byte được lấy bởi các tệp không phải thư mục trong mỗi thư mục bên dưới thư mục bắt đầu, ngoại trừ việc nó không tìm trong bất kỳ thư mục con CVS nào if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"1 Trong ví dụ tiếp theo, đi bộ từ dưới lên trên cây là điều cần thiết. không cho phép xóa thư mục trước khi thư mục trống if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"2 Đưa ra một import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories52 với các đối số import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories36, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories37, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories38, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories56, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()39 Unix New in version 3. 3 Changed in version 3. 6. Accepts a . Đã thay đổi trong phiên bản 3. 7. Đã thêm hỗ trợ cho đường dẫn. os. memfd_create(tên[ , . MFD_CLOEXEC flags=os.MFD_CLOEXEC])Tạo một tệp ẩn danh và trả về một bộ mô tả tệp đề cập đến nó. các cờ phải là một trong các hằng số import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories59 có sẵn trên hệ thống (hoặc kết hợp ORed theo bit của chúng). Theo mặc định, bộ mô tả tệp mới là Tên được cung cấp trong tên được sử dụng làm tên tệp và sẽ được hiển thị dưới dạng đích của liên kết tượng trưng tương ứng trong thư mục import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories60. Tên được hiển thị luôn có tiền tố là import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories61 và chỉ phục vụ cho mục đích gỡ lỗi. Tên không ảnh hưởng đến hành vi của bộ mô tả tệp và do đó, nhiều tệp có thể có cùng tên mà không có bất kỳ tác dụng phụ nào Linux >= 3. 17 với glibc >= 2. 27 New in version 3. 8 os. MFD_CLOEXECos. MFD_ALLOW_SEALINGos. MFD_HUGETLBos. MFD_HUGE_SHIFTos. MFD_HUGE_MASKos. MFD_HUGE_64KBos. MFD_HUGE_512KBos. MFD_HUGE_1MBos. MFD_HUGE_2MBos. MFD_HUGE_8MBos. MFD_HUGE_16MBos. MFD_HUGE_32MBos. MFD_HUGE_256MBos. MFD_HUGE_512MBos. MFD_HUGE_1GBos. MFD_HUGE_2GBos. MFD_HUGE_16GBNhững cờ này có thể được chuyển đến Linux >= 3. 17 với glibc >= 2. 27 Các cờ import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories63 chỉ khả dụng kể từ Linux 4. 14 New in version 3. 8 os. eventfd(initval[ , . EFD_CLOEXEC flags=os.EFD_CLOEXEC])Tạo và trả về một bộ mô tả tệp sự kiện. Các bộ mô tả tệp hỗ trợ thô và có kích thước bộ đệm là 8, , và tương tự. Xem trang man eventfd(2) để biết thêm thông tin. Theo mặc định, bộ mô tả tệp mới là initval is the initial value of the event counter. Giá trị ban đầu phải là số nguyên không dấu 32 bit. Xin lưu ý rằng giá trị ban đầu được giới hạn ở một int không dấu 32 bit mặc dù bộ đếm sự kiện là một số nguyên 64 bit không dấu với giá trị tối đa là 264-2 cờ có thể được xây dựng từ , , và Nếu được chỉ định và bộ đếm sự kiện khác không, trả về 1 và giảm bộ đếm đi một Nếu không được chỉ định và bộ đếm sự kiện khác không, trả về giá trị bộ đếm sự kiện hiện tại và đặt lại bộ đếm về 0 Nếu bộ đếm sự kiện bằng 0 và không được chỉ định, sẽ chặn tăng bộ đếm sự kiện. Ghi khối nếu thao tác ghi sẽ tăng bộ đếm lên giá trị lớn hơn 264-2 Example if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"3 Linux >= 2. 6. 27 với glibc >= 2. 8 New in version 3. 10 os. eventfd_read(fd)Đọc giá trị từ bộ mô tả tệp và trả về int không dấu 64 bit. Chức năng không xác minh rằng fd là một Linux >= 2. 6. 27 New in version 3. 10 os. eventfd_write(fd , value)Thêm giá trị vào một bộ mô tả tập tin. giá trị phải là một int không dấu 64 bit. Chức năng không xác minh rằng fd là một Linux >= 2. 6. 27 New in version 3. 10 os. EFD_CLOEXECĐặt cờ close-on-exec cho bộ mô tả tệp mới Linux >= 2. 6. 27 New in version 3. 10 os. EFD_NONBLOCKĐặt cờ trạng thái cho bộ mô tả tệp mới Linux >= 2. 6. 27 New in version 3. 10 os. EFD_SEMAPHORECung cấp ngữ nghĩa giống như semaphore để đọc từ bộ mô tả tệp. Khi đọc bộ đếm bên trong được giảm đi một Linux >= 2. 6. 30 New in version 3. 10 Thuộc tính mở rộng LinuxNew in version 3. 3 Các chức năng này chỉ có sẵn trên Linux os. getxattr(đường dẫn , thuộc tính, *, follow_symlinks=True)Trả về giá trị của thuộc tính thuộc tính hệ thống tập tin mở rộng cho đường dẫn. attribute can be bytes or str (directly or indirectly through the interface). Nếu là str, nó được mã hóa bằng mã hóa hệ thống tệp This function can support and Đưa ra một import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories87 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories89 Đã thay đổi trong phiên bản 3. 6. Chấp nhận đường dẫn và thuộc tính for. os. listxattr(đường dẫn=Không có, *, follow_symlinks=True)Trả về danh sách các thuộc tính hệ thống tệp mở rộng trên đường dẫn. Các thuộc tính trong danh sách được biểu diễn dưới dạng các chuỗi được giải mã bằng mã hóa hệ thống tệp. Nếu đường dẫn là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35, sẽ kiểm tra thư mục hiện tại This function can support and Đưa ra một import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories92 với lập luận try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37 Changed in version 3. 6. Accepts a . os. removexattr(đường dẫn , thuộc tính, *, follow_symlinks=True)Xóa thuộc tính thuộc tính hệ thống tệp mở rộng khỏi đường dẫn. thuộc tính phải là byte hoặc str (trực tiếp hoặc gián tiếp thông qua giao diện). Nếu nó là một chuỗi, nó được mã hóa bằng This function can support and Tăng một import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories95 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories89 Đã thay đổi trong phiên bản 3. 6. Chấp nhận đường dẫn và thuộc tính for. os. setxattr(đường dẫn , thuộc tính, value, flags=0, *, follow_symlinks=True)Set the extended filesystem attribute attribute on path to value. thuộc tính phải là byte hoặc str không có NUL nhúng (trực tiếp hoặc gián tiếp thông qua giao diện). Nếu nó là một str, nó được mã hóa bằng. cờ có thể là hoặc. Nếu được đưa ra và thuộc tính không tồn tại, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"002 sẽ được nâng lên. Nếu được cung cấp và thuộc tính đã tồn tại, thuộc tính sẽ không được tạo và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"004 sẽ tăng This function can support and Note Một lỗi trong các phiên bản nhân Linux nhỏ hơn 2. 6. 39 khiến đối số flags bị bỏ qua trên một số hệ thống tệp Tăng một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"005 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, import os from os.path import join, getsize for root, dirs, files in os.walk('python/Lib/email'): print(root, "consumes", end=" ") print(sum(getsize(join(root, name)) for name in files), end=" ") print("bytes in", len(files), "non-directory files") if 'CVS' in dirs: dirs.remove('CVS') # don't visit CVS directories89, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"75, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()98 Đã thay đổi trong phiên bản 3. 6. Chấp nhận đường dẫn và thuộc tính for. os. XATTR_SIZE_MAXKích thước tối đa giá trị của thuộc tính mở rộng có thể là. Hiện tại, đây là 64 KiB trên Linux os. XATTR_CREATEĐây là một giá trị có thể có cho đối số flags trong. Nó chỉ ra rằng hoạt động phải tạo ra một thuộc tính os. XATTR_REPLACEĐây là một giá trị có thể có cho đối số flags trong. Nó cho biết hoạt động phải thay thế một thuộc tính hiện có Process ManagementCác chức năng này có thể được sử dụng để tạo và quản lý các quy trình Các chức năng khác nhau lấy danh sách các đối số cho chương trình mới được tải vào quy trình. Trong mỗi trường hợp, đối số đầu tiên trong số này được chuyển đến chương trình mới dưới dạng tên riêng của nó chứ không phải là đối số mà người dùng có thể đã nhập vào một dòng lệnh. Đối với lập trình viên C, đây là ____25_______013 được chuyển đến ____25_______014 của chương trình. Ví dụ: if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"015 sẽ chỉ in if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"016 trên đầu ra tiêu chuẩn; os. hủy bỏ() Tạo tín hiệu if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"018 cho quy trình hiện tại. Trên Unix, hành vi mặc định là tạo ra một kết xuất lõi; . Xin lưu ý rằng việc gọi hàm này sẽ không gọi trình xử lý tín hiệu Python đã đăng ký cho if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"018 vớios. add_dll_directory(đường dẫn) Thêm đường dẫn vào đường dẫn tìm kiếm DLL Đường dẫn tìm kiếm này được sử dụng khi giải quyết các phụ thuộc cho các mô-đun mở rộng đã nhập (bản thân mô-đun được giải quyết thông qua ) và cũng bởi Xóa thư mục bằng cách gọi close() trên đối tượng được trả về hoặc sử dụng nó trong một câu lệnh Xem tài liệu của Microsoft để biết thêm thông tin về cách tải tệp DLL Tăng một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"025 với lập luận try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37 Windows Mới trong phiên bản 3. 8. Các phiên bản trước của CPython sẽ giải quyết các tệp DLL bằng hành vi mặc định cho quy trình hiện tại. This led to inconsistencies, such as only sometimes searching if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027 or the current working directory, and OS functions such as if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"028 having no effect. Trong 3. 8, hai cách chính mà tệp DLL được tải hiện sẽ ghi đè rõ ràng hành vi trên toàn quy trình để đảm bảo tính nhất quán. Xem thông tin về cập nhật thư viện os. execl(đường dẫn , arg0 . , arg1, ...)os. execle(đường dẫn , arg0 . , arg1, ... , env)os. execlp(tệp , arg0 . , arg1, ...)os. execlpe(tệp , arg0 . , arg1, ... , env)os. execv(đường dẫn , args . )os.execve(path , args . , env)os.execvp(file , args . )os.execvpe(tệp , args, env)Tất cả các chức năng này thực hiện một chương trình mới, thay thế quy trình hiện tại; . On Unix, the new executable is loaded into the current process, and will have the same process id as the caller. Lỗi sẽ được báo cáo là ngoại lệ Quy trình hiện tại được thay thế ngay lập tức. Các đối tượng và bộ mô tả tệp đang mở không được xóa, vì vậy nếu có thể có dữ liệu được lưu vào bộ đệm trên các tệp đang mở này, bạn nên xóa chúng bằng cách sử dụng if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"030 hoặc trước khi gọi một hàm Các biến thể “l” và “v” của các hàm khác nhau về cách truyền đối số dòng lệnh. Các biến thể “l” có lẽ là dễ làm việc nhất nếu số lượng tham số được cố định khi viết mã; . Các biến thể của v v rất tốt khi số lượng tham số thay đổi, với các đối số được truyền trong một danh sách hoặc bộ dữ liệu dưới dạng tham số args. Trong cả hai trường hợp, các đối số của tiến trình con phải bắt đầu bằng tên của lệnh đang được chạy, nhưng điều này không được thực thi Các biến thể bao gồm một chữ “p” ở gần cuối (, , và ) sẽ sử dụng biến môi trường if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027 để định vị tệp chương trình. Khi môi trường đang được thay thế (sử dụng một trong các biến thể, được thảo luận trong đoạn tiếp theo), môi trường mới được sử dụng làm nguồn của biến if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027. Các biến thể khác, , , và , sẽ không sử dụng biến if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027 để định vị tệp thực thi; Đối với , , và (lưu ý rằng tất cả các giá trị này đều kết thúc bằng “e”), tham số env phải là một ánh xạ được sử dụng để xác định các biến môi trường cho quy trình mới (các biến này được sử dụng thay cho môi trường của quy trình hiện tại); Đối với một số nền tảng, đường dẫn cũng có thể được chỉ định làm bộ mô tả tệp mở. Chức năng này có thể không được hỗ trợ trên nền tảng của bạn; . Nếu nó không có sẵn, sử dụng nó sẽ tăng Tăng một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"058 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"060, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"061 Unix, Windows, not Emscripten, not WASI Mới trong phiên bản 3. 3. Đã thêm hỗ trợ để chỉ định đường dẫn làm bộ mô tả tệp mở cho. Changed in version 3. 6. Accepts a . os. _exit(n)Thoát khỏi quy trình với trạng thái n, mà không gọi trình xử lý dọn dẹp, xóa bộ đệm stdio, v.v. Note The standard way to exit is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"063. thông thường chỉ nên được sử dụng trong tiến trình con sau một Các mã thoát sau đây được xác định và có thể được sử dụng với , mặc dù chúng không bắt buộc. Chúng thường được sử dụng cho các chương trình hệ thống được viết bằng Python, chẳng hạn như chương trình gửi lệnh bên ngoài của máy chủ thư Note Một số trong số này có thể không khả dụng trên tất cả các nền tảng Unix, vì có một số biến thể. Các hằng số này được xác định ở nơi chúng được xác định bởi nền tảng cơ bản os. EX_OKMã thoát có nghĩa là không có lỗi xảy ra. Có thể được lấy từ giá trị được xác định của if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"067 trên một số nền tảng. Nói chung có giá trị bằng không Hệ điều hành Unix, Windows os. EX_USAGEMã thoát có nghĩa là lệnh đã được sử dụng không chính xác, chẳng hạn như khi đưa ra số lượng đối số sai Unix, không phải Emscripten, không phải WASI os. EX_DATAERRMã thoát có nghĩa là dữ liệu đầu vào không chính xác Unix, không phải Emscripten, không phải WASI os. EX_NOINPUTMã thoát có nghĩa là tệp đầu vào không tồn tại hoặc không thể đọc được Unix, không phải Emscripten, không phải WASI os. EX_NOUSERMã thoát có nghĩa là người dùng được chỉ định không tồn tại Unix, không phải Emscripten, không phải WASI os. EX_NOHOSTMã thoát có nghĩa là máy chủ được chỉ định không tồn tại Unix, không phải Emscripten, không phải WASI os. EX_UNAVAILABLEMã thoát có nghĩa là dịch vụ được yêu cầu không khả dụng Unix, không phải Emscripten, không phải WASI os. EX_SOFTWAREMã thoát có nghĩa là đã phát hiện lỗi phần mềm nội bộ Unix, không phải Emscripten, không phải WASI os. EX_OSERRMã thoát có nghĩa là đã phát hiện thấy lỗi hệ điều hành, chẳng hạn như không thể rẽ nhánh hoặc tạo đường ống Unix, không phải Emscripten, không phải WASI os. EX_OSFILEMã thoát có nghĩa là một số tệp hệ thống không tồn tại, không thể mở được hoặc có một số loại lỗi khác Unix, không phải Emscripten, không phải WASI os. EX_CANTCREATMã thoát có nghĩa là không thể tạo tệp đầu ra do người dùng chỉ định Unix, không phải Emscripten, không phải WASI os. EX_IOERRMã thoát có nghĩa là đã xảy ra lỗi khi thực hiện I/O trên một số tệp Unix, không phải Emscripten, không phải WASI os. EX_TEMPFAILMã thoát có nghĩa là đã xảy ra lỗi tạm thời. Điều này cho biết điều gì đó có thể không thực sự là lỗi, chẳng hạn như kết nối mạng không thể thực hiện được trong một thao tác có thể thử lại Unix, không phải Emscripten, không phải WASI os. EX_PROTOCOLMã thoát có nghĩa là trao đổi giao thức là bất hợp pháp, không hợp lệ hoặc không hiểu Unix, không phải Emscripten, không phải WASI os. EX_NOPERMMã thoát có nghĩa là không có đủ quyền để thực hiện thao tác (nhưng không dành cho các sự cố hệ thống tệp) Unix, không phải Emscripten, không phải WASI os. EX_CONFIGMã thoát có nghĩa là đã xảy ra một số loại lỗi cấu hình Unix, không phải Emscripten, không phải WASI os. EX_NOTFOUNDMã thoát có nghĩa là "không tìm thấy mục nhập" Unix, không phải Emscripten, không phải WASI os. ngã ba()Fork a child process. Return try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()83 in the child and the child’s process id in the parent. Nếu một lỗi xảy ra được nâng lên Lưu ý rằng một số nền tảng bao gồm FreeBSD <= 6. 3 và Cygwin đã biết các sự cố khi sử dụng try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()6 từ một luồng Tăng if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"071 mà không có đối số Đã thay đổi trong phiên bản 3. 8. Việc gọi try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()6 trong một trình thông dịch phụ không còn được hỗ trợ nữa ( được nâng lên). Cảnh báo Xem các ứng dụng sử dụng mô-đun SSL với fork() Unix, không phải Emscripten, không phải WASI os. phân nhánh()Chia nhánh một tiến trình con, sử dụng thiết bị đầu cuối giả mới làm thiết bị đầu cuối điều khiển của con. Trả về một cặp if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"075, trong đó pid là try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()83 ở phần tử con, id tiến trình của phần tử con mới ở phần gốc và fd là bộ mô tả tệp của phần cuối chính của thiết bị đầu cuối giả. Để có cách tiếp cận di động hơn, hãy sử dụng mô-đun. Nếu một lỗi xảy ra được nâng lên Tăng một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"079 mà không có đối số Đã thay đổi trong phiên bản 3. 8. Gọi if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"080 trong một trình thông dịch phụ không còn được hỗ trợ nữa ( được nâng lên). Unix, không phải Emscripten, không phải WASI os. kill(pid , sig , /)Gửi tín hiệu sig đến quá trình pid. Các hằng số cho các tín hiệu cụ thể có sẵn trên nền tảng máy chủ được xác định trong mô-đun các cửa sổ. Các tín hiệu và là các tín hiệu đặc biệt chỉ có thể được gửi đến các tiến trình điều khiển có chung một cửa sổ điều khiển, e. g. , một số quy trình con. Bất kỳ giá trị nào khác cho sig sẽ khiến quy trình bị hủy vô điều kiện bởi API TerminateProcess và mã thoát sẽ được đặt thành sig. Phiên bản Windows của bổ sung xử lý quá trình bị giết Xem thêm Nâng cao một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"087 với các đối số if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"088, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"089 Unix, Windows, not Emscripten, not WASI Mới trong phiên bản 3. 2. Hỗ trợ Windows. os. killpg(pgid , sig, /)Gửi sig tín hiệu đến pgid nhóm quy trình Tăng một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"090 với các đối số if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"091, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"089 Unix, không phải Emscripten, không phải WASI os. tốt(tăng , /)Thêm phần gia tăng vào “sự tốt đẹp” của quy trình. Return the new niceness Unix, không phải Emscripten, không phải WASI os. pidfd_open(pid , cờ=0)Trả về một bộ mô tả tệp đề cập đến quá trình pid. Bộ mô tả này có thể được sử dụng để thực hiện quản lý quy trình mà không cần các cuộc đua và tín hiệu. Đối số flags được cung cấp cho các tiện ích mở rộng trong tương lai; Xem trang man pidfd_open(2) để biết thêm chi tiết Linux >= 5. 3 Mới trong phiên bản 3. 9 os. khóa(op , /)Khóa các đoạn chương trình vào bộ nhớ. Giá trị của op (được xác định trong if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"093) xác định phân đoạn nào bị khóa Unix, không phải Emscripten, không phải WASI os. popen(cmd , chế độ='r', buffering=- 1)Mở một đường ống đến hoặc từ lệnh cmd. Giá trị trả về là một đối tượng tệp mở được kết nối với đường ống, có thể được đọc hoặc ghi tùy thuộc vào chế độ là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"094 (mặc định) hay if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"095. Đối số đệm có cùng ý nghĩa với đối số tương ứng với hàm tích hợp. Đối tượng tệp được trả về đọc hoặc ghi chuỗi văn bản thay vì byte Phương thức if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"097 trả về nếu quy trình con đã thoát thành công hoặc mã trả về của quy trình con nếu có lỗi. Trên các hệ thống POSIX, nếu mã trả về là số dương, mã này biểu thị giá trị trả về của quy trình được dịch trái một byte. Nếu mã trả về là số âm, quá trình đã bị kết thúc bởi tín hiệu được cung cấp bởi giá trị phủ định của mã trả về. (Ví dụ: giá trị trả về có thể là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"099 nếu quy trình con bị hủy. ) Trên các hệ thống Windows, giá trị trả về chứa mã trả về số nguyên đã ký từ tiến trình con Trên Unix, có thể được sử dụng để chuyển đổi kết quả của phương thức if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"097 (trạng thái thoát) thành mã thoát nếu nó không phải là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35. Trên Windows, kết quả của phương pháp if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"097 trực tiếp là mã thoát (hoặc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35) Điều này được thực hiện bằng cách sử dụng ; không phải Emscripten, không phải WASI Note Các mã hóa ảnh hưởng được sử dụng cho nội dung cmd và đường ống là một trình bao bọc đơn giản xung quanh. Sử dụng hoặc để kiểm soát các tùy chọn như mã hóa os. posix_spawn(đường dẫn , argv, env, *, file_actions=None, setpgroup=None, resetids=False, setsid=False, setsigmask=(), setsigdef=(), scheduler=None)Kết thúc API thư viện if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"110 C để sử dụng từ Python Hầu hết người dùng nên sử dụng thay vì Đường dẫn đối số chỉ vị trí, args và env tương tự như Tham số đường dẫn là đường dẫn đến tệp thực thi. Đường dẫn phải chứa một thư mục. Sử dụng để truyền tệp thực thi mà không có thư mục Đối số file_actions có thể là một chuỗi các bộ dữ liệu mô tả các hành động sẽ thực hiện trên các bộ mô tả tệp cụ thể trong quy trình con giữa các bước triển khai thư viện C try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()6 và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"116. The first item in each tuple must be one of the three type indicator listed below describing the remaining tuple elementsos. POSIX_SPAWN_OPEN ( if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"117, fd, đường dẫn, cờ, chế độ) Biểu diễn if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"118os. POSIX_SPAWN_CLOSE ( if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"119, fd) Biểu diễn if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"120os. POSIX_SPAWN_DUP2 ( if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"121, fd, new_fd) Biểu diễn if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"122 Các bộ dữ liệu này tương ứng với các lệnh gọi API của thư viện C if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"123, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"124 và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"125 được sử dụng để chuẩn bị cho chính lệnh gọi if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"110 Đối số setpgroup sẽ đặt nhóm quy trình của con thành giá trị được chỉ định. Nếu giá trị được chỉ định là 0, ID nhóm quy trình con sẽ được tạo giống với ID quy trình của nó. Nếu giá trị của setpgroup không được đặt, thì phần tử con sẽ kế thừa ID nhóm tiến trình của cha mẹ. Đối số này tương ứng với cờ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"127 của thư viện C Nếu đối số resetids là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04, nó sẽ đặt lại UID và GID hiệu quả của quy trình con thành UID và GID thực của quy trình gốc. If the argument is if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92, then the child retains the effective UID and GID of the parent. Trong cả hai trường hợp, nếu các bit cấp phép set-user-ID và set-group-ID được bật trên tệp thực thi, hiệu ứng của chúng sẽ ghi đè cài đặt của UID và GID hiệu quả. Đối số này tương ứng với cờ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"130 của thư viện C Nếu đối số setsid là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04, nó sẽ tạo ID phiên mới cho if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"132. setsid yêu cầu cờ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"133 hoặc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"134. Nếu không, được nâng lên Đối số setsignmask sẽ đặt mặt nạ tín hiệu thành bộ tín hiệu được chỉ định. Nếu tham số không được sử dụng, thì đứa trẻ sẽ thừa hưởng mặt nạ tín hiệu của cha mẹ. Đối số này tương ứng với cờ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"136 của thư viện C Đối số sigdef sẽ đặt lại bố cục của tất cả các tín hiệu trong tập hợp được chỉ định. Đối số này tương ứng với cờ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"137 của thư viện C Đối số bộ lập lịch phải là một bộ chứa chính sách bộ lập lịch (tùy chọn) và một thể hiện của các tham số bộ lập lịch. Giá trị của if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35 ở vị trí của chính sách lập lịch trình cho biết rằng điều đó không được cung cấp. Đối số này là sự kết hợp của thư viện C cờ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"140 và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"141 Đưa ra một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"142 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"144, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"061 New in version 3. 8 Unix, không phải Emscripten, không phải WASI os. posix_spawnp(đường dẫn , argv, env, *, file_actions=None, setpgroup=None, resetids=False, setsid=False, setsigmask=(), setsigdef=(), scheduler=None)Kết thúc API thư viện if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"114 C để sử dụng từ Python Tương tự ngoại trừ việc hệ thống tìm kiếm tệp thực thi trong danh sách các thư mục được chỉ định bởi biến môi trường if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027 (theo cách tương tự như đối với if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"149) Đưa ra một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"142 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"144, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"061 New in version 3. 8 POSIX, không phải Emscripten, không phải WASI See documentation os. register_at_fork(* , trước=None, after_in_parent=None, after_in_child=None)Đăng ký các cuộc gọi để được thực thi khi một quy trình con mới được rẽ nhánh bằng cách sử dụng hoặc các API nhân bản quy trình tương tự. Các tham số là tùy chọn và chỉ từ khóa. Mỗi chỉ định một điểm gọi khác nhau
Các cuộc gọi này chỉ được thực hiện nếu điều khiển dự kiến sẽ quay trở lại trình thông dịch Python. Một lần khởi chạy thông thường sẽ không kích hoạt chúng vì đứa trẻ sẽ không vào lại trình thông dịch Các chức năng đã đăng ký để thực hiện trước khi rẽ nhánh được gọi theo thứ tự đăng ký ngược lại. Các chức năng được đăng ký để thực thi sau khi rẽ nhánh (ở cha hoặc ở con) được gọi theo thứ tự đăng ký Lưu ý rằng các cuộc gọi try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()6 được thực hiện bởi mã C của bên thứ ba có thể không gọi các chức năng đó, trừ khi nó gọi rõ ràng và Không có cách nào để hủy đăng ký một chức năng Unix, không phải Emscripten, không phải WASI Mới trong phiên bản 3. 7 os. sinh sản(chế độ , đường dẫn . , ...)os. đẻ trứng(chế độ , đường dẫn . , ... , env)os. spawnlp(chế độ , tệp . , ...)os. spawnlpe(mode , file , . , env)os. spawnv(chế độ , đường dẫn . , args)os.đường sinh sản(chế độ , . , args, env)os.spawnvp(chế độ , tệp . , args)os.spawnvpe(chế độ , tệp, args, env)Thực hiện đường dẫn chương trình trong một quy trình mới (Lưu ý rằng mô-đun cung cấp các phương tiện mạnh mẽ hơn để sinh ra các quy trình mới và truy xuất kết quả của chúng; sử dụng mô-đun đó tốt hơn là sử dụng các chức năng này. Đặc biệt kiểm tra phần. ) Nếu chế độ là , hàm này trả về id tiến trình của tiến trình mới; . Trên Windows, process id thực sự sẽ là process handle, vì vậy có thể được sử dụng với chức năng Lưu ý trên VxWorks, chức năng này không trả về if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"164 khi quy trình mới bị hủy. Thay vào đó, nó làm tăng ngoại lệ OSError Các biến thể “l” và “v” của các hàm khác nhau về cách truyền đối số dòng lệnh. Các biến thể “l” có lẽ là dễ làm việc nhất nếu số lượng tham số được cố định khi viết mã; . Các biến thể của v v rất tốt khi số lượng tham số thay đổi, với các đối số được truyền trong một danh sách hoặc bộ dữ liệu dưới dạng tham số args. Trong cả hai trường hợp, các đối số của tiến trình con phải bắt đầu bằng tên của lệnh đang chạy Các biến thể bao gồm chữ “p” thứ hai ở gần cuối (, , và ) sẽ sử dụng biến môi trường if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027 để định vị tệp chương trình. Khi môi trường đang được thay thế (sử dụng một trong các biến thể, được thảo luận trong đoạn tiếp theo), môi trường mới được sử dụng làm nguồn của biến if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027. Các biến thể khác, , , và , sẽ không sử dụng biến if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027 để định vị tệp thực thi; Đối với , , và (lưu ý rằng tất cả các giá trị này đều kết thúc bằng “e”), tham số env phải là một ánh xạ được sử dụng để xác định các biến môi trường cho quy trình mới (chúng được sử dụng thay cho môi trường của quy trình hiện tại); . Note that keys and values in the env dictionary must be strings; invalid keys or values will cause the function to fail, with a return value of if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"189 Như một ví dụ, các cuộc gọi sau đến và là tương đương if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"4 Nâng cao một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"192 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()38, try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"060, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"061 Unix, Windows, not Emscripten, not WASI , , và không khả dụng trên Windows. và không an toàn cho luồng trên Windows; Changed in version 3. 6. Accepts a . os. P_NOWAITos. P_NOWAITOCác giá trị có thể có cho tham số chế độ đối với họ hàm. Nếu một trong hai giá trị này được cung cấp, các hàm if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"205 sẽ trả về ngay khi quy trình mới được tạo, với id quy trình là giá trị trả về Hệ điều hành Unix, Windows os. P_WAITGiá trị có thể cho tham số chế độ đối với họ hàm. Nếu điều này được đưa ra dưới dạng chế độ, các hàm if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"205 sẽ không trả về cho đến khi quy trình mới chạy hoàn tất và sẽ trả về mã thoát của quy trình khi chạy thành công hoặc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"164 nếu một tín hiệu giết chết quy trình Hệ điều hành Unix, Windows os. P_DETACHos. P_OVERLAYCác giá trị có thể có cho tham số chế độ đối với họ hàm. Những thứ này ít di động hơn những thứ được liệt kê ở trên. tương tự như , nhưng quy trình mới được tách ra khỏi bảng điều khiển của quy trình gọi. Nếu được sử dụng, quy trình hiện tại sẽ được thay thế; Windows os. tệp bắt đầu(đường dẫn[ , operation][, arguments][, cwd][, show_cmd])Bắt đầu một tệp với ứng dụng được liên kết của nó Khi thao tác không được chỉ định hoặc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"214, thao tác này giống như bấm đúp vào tệp trong Windows Explorer hoặc đặt tên tệp làm đối số cho lệnh bắt đầu từ trình bao lệnh tương tác. tệp được mở bằng bất kỳ ứng dụng nào (nếu có) phần mở rộng của nó được liên kết Khi một thao tác khác được đưa ra, nó phải là một “động từ mệnh lệnh” chỉ định những gì sẽ được thực hiện với tệp. Các động từ phổ biến được Microsoft ghi lại là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"215 và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"216 (được sử dụng trên tệp) cũng như if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"217 và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"218 (được sử dụng trên thư mục) Khi khởi chạy một ứng dụng, hãy chỉ định các đối số được truyền dưới dạng một chuỗi. Đối số này có thể không có tác dụng khi sử dụng chức năng này để khởi chạy tài liệu Thư mục làm việc mặc định được kế thừa, nhưng có thể bị ghi đè bởi đối số cwd. Đây phải là một đường dẫn tuyệt đối. Một đường dẫn tương đối sẽ được giải quyết theo đối số này Sử dụng show_cmd để ghi đè kiểu cửa sổ mặc định. Điều này có ảnh hưởng gì hay không sẽ phụ thuộc vào ứng dụng được khởi chạy. Giá trị là số nguyên như được hỗ trợ bởi hàm Win32 if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"219 trả về ngay khi ứng dụng được liên kết được khởi chạy. There is no option to wait for the application to close, and no way to retrieve the application’s exit status. Tham số đường dẫn có liên quan đến thư mục hiện tại hoặc cwd. Nếu bạn muốn sử dụng đường dẫn tuyệt đối, hãy đảm bảo rằng ký tự đầu tiên không phải là dấu gạch chéo ( if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"221) Sử dụng hoặc chức năng để đảm bảo rằng đường dẫn được mã hóa chính xác cho Win32 Để giảm chi phí khởi động trình thông dịch, chức năng Win32 if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"219 không được giải quyết cho đến khi chức năng này được gọi lần đầu tiên. Nếu chức năng không thể được giải quyết, sẽ được nâng lên Tăng một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"226 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"228 Nâng cao một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"229 với các đối số try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()37, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"228, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"232, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"233, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"234 Windows Đã thay đổi trong phiên bản 3. 10. Đã thêm các đối số, đối số cwd và show_cmd cũng như sự kiện kiểm tra if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"229. os. hệ thống(lệnh) Thực thi lệnh (một chuỗi) trong một lớp con. Điều này được thực hiện bằng cách gọi chức năng C tiêu chuẩn if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"236 và có những hạn chế tương tự. Thay đổi thành , v.v. không được phản ánh trong môi trường của lệnh được thực thi. Nếu lệnh tạo ra bất kỳ đầu ra nào, nó sẽ được gửi đến luồng đầu ra tiêu chuẩn của trình thông dịch. Tiêu chuẩn C không chỉ định ý nghĩa của giá trị trả về của hàm C, vì vậy giá trị trả về của hàm Python phụ thuộc vào hệ thống Trên Unix, giá trị trả về là trạng thái thoát của quy trình được mã hóa theo định dạng được chỉ định cho Trên Windows, giá trị trả về là giá trị được shell hệ thống trả về sau khi chạy lệnh. Shell được cung cấp bởi biến môi trường Windows if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"239. nó thường là cmd. exe, trả về trạng thái thoát của lệnh chạy; Mô-đun này cung cấp các phương tiện mạnh mẽ hơn để tạo ra các quy trình mới và truy xuất kết quả của chúng; . Xem phần trong tài liệu để biết một số công thức nấu ăn hữu ích Trên Unix, có thể được sử dụng để chuyển đổi kết quả (trạng thái thoát) thành mã thoát. Trên Windows, kết quả trực tiếp là mã thoát Đưa ra một if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"243 với lập luận if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"244 Unix, Windows, not Emscripten, not WASI os. times()Trả về thời gian xử lý toàn cầu hiện tại. Giá trị trả về là một đối tượng có năm thuộc tính
Đối với khả năng tương thích ngược, đối tượng này cũng hoạt động giống như một bộ năm bộ chứa if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"245, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"246, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"247, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"248 và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"249 theo thứ tự đó Xem trang thủ công Unix times(2) và times(3) trên Unix hoặc GetProcessTimes MSDN trên Windows. Trên Windows, chỉ có if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"245 và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"246 được biết đến; Hệ điều hành Unix, Windows Changed in version 3. 3. Return type changed from a tuple to a tuple-like object with named attributes. os. chờ đã()Đợi quá trình con hoàn thành và trả về một bộ chứa chỉ báo trạng thái thoát và pid của nó. một số 16 bit, có byte thấp là số tín hiệu đã giết quá trình và byte cao là trạng thái thoát (nếu số tín hiệu bằng 0); Nếu không có đứa trẻ nào có thể chờ đợi, được nuôi dưỡng có thể được sử dụng để chuyển đổi trạng thái thoát thành mã thoát Unix, không phải Emscripten, không phải WASI See also Các hàm if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"259 khác được ghi lại dưới đây có thể được sử dụng để chờ hoàn thành một tiến trình con cụ thể và có nhiều tùy chọn hơn. là cái duy nhất cũng có sẵn trên Windowsos. waitid(idtype , id , options , /) Đợi quá trình con hoàn thành idtype có thể là , , hoặc (trên Linux). Việc giải thích id phụ thuộc vào nó; tùy chọn là tổ hợp cờ OR. Ít nhất một trong số , hoặc được yêu cầu; Giá trị trả về là một đối tượng đại diện cho dữ liệu chứa trong cấu trúc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"270 với các thuộc tính sau
Nếu được chỉ định và không có trẻ em phù hợp trong tiểu bang được yêu cầu, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35 được trả lại. Mặt khác, nếu không có con nào phù hợp có thể chờ đợi, sẽ được nuôi Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. waitpid(pid , tùy chọn, /)Các chi tiết của chức năng này khác nhau trên Unix và Windows On Unix. Đợi quá trình con được cung cấp bởi id quá trình pid hoàn thành và trả về một bộ chứa id quá trình của nó và chỉ báo trạng thái thoát (được mã hóa thành for ). Ngữ nghĩa của lệnh gọi bị ảnh hưởng bởi giá trị của các tùy chọn số nguyên, giá trị này sẽ là ____0_______83 cho hoạt động bình thường Nếu pid lớn hơn try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()83, hãy yêu cầu thông tin trạng thái cho quy trình cụ thể đó. Nếu pid là try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()83, yêu cầu dành cho trạng thái của bất kỳ phần tử con nào trong nhóm quy trình của quy trình hiện tại. Nếu pid là with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)32, yêu cầu liên quan đến bất kỳ phần tử con nào của quy trình hiện tại. Nếu pid nhỏ hơn with os.scandir(path) as it: for entry in it: if not entry.name.startswith('.') and entry.is_file(): print(entry.name)32, trạng thái được yêu cầu cho bất kỳ quy trình nào trong nhóm quy trình if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"289 (giá trị tuyệt đối của pid) tùy chọn là tổ hợp cờ OR. Nếu nó chứa và không có trẻ em phù hợp trong trạng thái được yêu cầu, thì trả lại if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"291. Mặt khác, nếu không có con nào phù hợp có thể chờ đợi, sẽ được nuôi. Các tùy chọn khác có thể được sử dụng là và Trên Windows. Đợi hoàn thành một quy trình do pid xử lý quy trình đưa ra và trả về một bộ chứa pid và trạng thái thoát của nó được dịch chuyển sang trái 8 bit (việc dịch chuyển giúp sử dụng chức năng đa nền tảng dễ dàng hơn). Một pid nhỏ hơn hoặc bằng try: fp = open("myfile") except PermissionError: return "some default data" else: with fp: return fp.read()83 không có ý nghĩa đặc biệt trên Windows và đưa ra một ngoại lệ. Giá trị của các tùy chọn số nguyên không có tác dụng. pid có thể đề cập đến bất kỳ quy trình nào có id được biết, không nhất thiết phải là quy trình con. Các chức năng được gọi với xử lý quy trình phù hợp trả về có thể được sử dụng để chuyển đổi trạng thái thoát thành mã thoát Unix, Windows, not Emscripten, not WASI Changed in version 3. 5. If the system call is interrupted and the signal handler does not raise an exception, the function now retries the system call instead of raising an exception (see PEP 475 for the rationale). os. chờ3(tùy chọn)Tương tự với , ngoại trừ không có đối số id quy trình nào được đưa ra và bộ 3 phần tử chứa id quy trình con, chỉ báo trạng thái thoát và thông tin sử dụng tài nguyên được trả về. Refer to for details on resource usage information. Đối số tùy chọn giống như đối số được cung cấp cho và có thể được sử dụng để chuyển đổi trạng thái thoát thành mã thoát Unix, không phải Emscripten, không phải WASI os. wait4(pid , tùy chọn)Tương tự như , ngoại trừ bộ 3 phần tử, chứa id tiến trình con, chỉ báo trạng thái thoát và thông tin sử dụng tài nguyên được trả về. Tham khảo để biết chi tiết về thông tin sử dụng tài nguyên. Các đối số giống như các đối số được cung cấp cho có thể được sử dụng để chuyển đổi trạng thái thoát thành mã thoát Unix, không phải Emscripten, không phải WASI os. P_PIDos. P_PGIDos. P_ALLos. P_PIDFDĐây là những giá trị có thể có cho idtype trong. Chúng ảnh hưởng đến cách hiểu id
Unix, không phải Emscripten, không phải WASI Note if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"264 chỉ khả dụng trên Linux >= 5. 4 New in version 3. 3 Mới trong phiên bản 3. 9. The if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"264 constant. os. WCONTINUED This options flag for , , , and causes child processes to be reported if they have been continued from a job control stop since they were last reported Unix, không phải Emscripten, không phải WASI os. WEXITEDThis options flag for causes child processes that have terminated to be reported The other if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"323 functions always report children that have terminated, so this option is not available for them Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. WSTOPPEDThis options flag for causes child processes that have been stopped by the delivery of a signal to be reported This option is not available for the other if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"323 functions Unix, không phải Emscripten, không phải WASI New in version 3. 3 os. WUNTRACEDThis options flag for , , and causes child processes to also be reported if they have been stopped but their current state has not been reported since they were stopped Tùy chọn này không có sẵn cho Unix, không phải Emscripten, không phải WASI os. WNOHANGCờ tùy chọn này khiến , , và quay lại ngay lập tức nếu không có trạng thái tiến trình con ngay lập tức Unix, không phải Emscripten, không phải WASI os. CHỜ ĐỢICờ tùy chọn này khiến cho phần tử con ở trạng thái có thể chờ được, để có thể sử dụng cuộc gọi if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"259 sau đó để truy xuất lại thông tin trạng thái phần tử con This option is not available for the other if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"323 functions Unix, không phải Emscripten, không phải WASI os. CLD_EXITEDos. CLD_KILLEDos. CLD_DUMPEDos. CLD_TRAPPEDos. CLD_STOPPEDos. CLD_TIẾP TỤCĐây là những giá trị có thể có cho if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"276 trong kết quả được trả về bởi Unix, không phải Emscripten, không phải WASI New in version 3. 3 Đã thay đổi trong phiên bản 3. 9. Đã thêm và các giá trị. os. waitstatus_to_exitcode(trạng thái)Chuyển đổi trạng thái chờ thành mã thoát Trên Unix
Trên Windows, trạng thái trả về được dịch chuyển sang phải 8 bit Trên Unix, nếu quá trình đang được theo dõi hoặc nếu được gọi với tùy chọn, trước tiên người gọi phải kiểm tra xem if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"349 có đúng không. This function must not be called if if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"349 is true See also , , , , , chức năng Unix, Windows, not Emscripten, not WASI Mới trong phiên bản 3. 9 Các hàm sau lấy mã trạng thái quy trình như được trả về bởi , hoặc dưới dạng tham số. Chúng có thể được sử dụng để xác định bố trí của một quá trình os. WCOREDUMP(trạng thái , /)Trả lại if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 nếu kết xuất lõi được tạo cho quy trình, nếu không thì trả lại if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 Chức năng này chỉ nên được sử dụng nếu là đúng Unix, không phải Emscripten, không phải WASI os. ĐANG TIẾP TỤC(trạng thái)Trả lại if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 nếu đứa trẻ bị dừng đã được tiếp tục bằng cách chuyển giao (nếu quá trình được tiếp tục từ điểm dừng kiểm soát công việc), nếu không thì trả lại if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 xem tùy chọn Unix, không phải Emscripten, không phải WASI os. ĐÃ NGỪNG(trạng thái)Trả lại if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 nếu quá trình bị dừng do gửi tín hiệu, nếu không thì trả lại if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 chỉ trả về if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 nếu cuộc gọi đã được thực hiện bằng cách sử dụng tùy chọn hoặc khi quy trình đang được theo dõi (xem ptrace(2)) Unix, không phải Emscripten, không phải WASI os. WIFSIGNALED(trạng thái)Return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 if the process was terminated by a signal, otherwise return if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"92 Unix, không phải Emscripten, không phải WASI os. ĐÃ CÓ VỢ(trạng thái)Trả lại if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"04 nếu quá trình đã thoát kết thúc bình thường, nghĩa là bằng cách gọi if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"376 hoặc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"064 hoặc bằng cách quay lại từ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"014; Unix, không phải Emscripten, không phải WASI os. WEXITSTATUS(trạng thái)Trả lại trạng thái thoát quy trình Chức năng này chỉ nên được sử dụng nếu là đúng Unix, không phải Emscripten, không phải WASI os. WSTOPSIG(trạng thái)Trả lại tín hiệu khiến quá trình dừng lại Chức năng này chỉ nên được sử dụng nếu là đúng Unix, không phải Emscripten, không phải WASI os. WTERMSIG(trạng thái)Trả về số lượng tín hiệu khiến quá trình kết thúc Chức năng này chỉ nên được sử dụng nếu là đúng Unix, không phải Emscripten, không phải WASI Giao diện bộ hẹn giờCác chức năng này kiểm soát cách hệ điều hành phân bổ thời gian CPU cho một quy trình. Chúng chỉ khả dụng trên một số nền tảng Unix. Để biết thêm thông tin chi tiết, hãy tham khảo các trang Unix của bạn New in version 3. 3 Các chính sách lập lịch trình sau đây được hiển thị nếu chúng được hệ điều hành hỗ trợ os. SCHED_OTHERChính sách lập lịch trình mặc định os. SCHED_BATCHChính sách lập lịch cho các quy trình sử dụng nhiều CPU cố gắng duy trì tính tương tác trên phần còn lại của máy tính os. SCHED_IDLEChính sách lập lịch trình cho các tác vụ nền có mức độ ưu tiên cực thấp os. SCHED_SPORADICChính sách lập lịch cho các chương trình máy chủ lẻ tẻ os. SCHED_FIFOChính sách lập kế hoạch First In First Out os. SCHED_RRChính sách lập lịch quay vòng os. SCHED_RESET_ON_FORKCờ này có thể được HOẶC với bất kỳ chính sách lập lịch trình nào khác. Khi một quy trình có cờ này đặt rẽ nhánh, chính sách lập lịch biểu và mức độ ưu tiên của quy trình con được đặt lại về mặc định lớp os. sched_param(sched_priority)Lớp này đại diện cho các tham số lập lịch có thể điều chỉnh được sử dụng trong , và. nó là bất biến Hiện tại, chỉ có một tham số có thể sched_priorityMức độ ưu tiên lập lịch cho chính sách lập lịch os. sched_get_priority_min(chính sách)Nhận giá trị ưu tiên tối thiểu cho chính sách. chính sách là một trong những hằng số chính sách lập lịch trình ở trên os. sched_get_priority_max(chính sách)Nhận giá trị ưu tiên tối đa cho chính sách. chính sách là một trong những hằng số chính sách lập lịch trình ở trên os. sched_setscheduler(pid , policy , param , /)Set the scheduling policy for the process with PID pid. Một pid của 0 có nghĩa là quá trình gọi. chính sách là một trong những hằng số chính sách lập lịch trình ở trên. param là một ví dụ os. sched_getscheduler(pid , /)Trả về chính sách lập lịch trình cho quy trình với PID pid. Một pid của 0 có nghĩa là quá trình gọi. Kết quả là một trong những hằng số chính sách lập lịch ở trên os. sched_setparam(pid , param, /)Đặt tham số lập lịch cho process với PID pid. Một pid của 0 có nghĩa là quá trình gọi. param là một ví dụ os. sched_getparam(pid , /)Trả về các tham số lập lịch làm ví dụ cho quy trình với PID pid. Một pid của 0 có nghĩa là quá trình gọi os. sched_rr_get_interval(pid , /)Trả về lượng tử quay vòng trong vài giây cho quá trình với PID pid. Một pid của 0 có nghĩa là quá trình gọi os. sched_yield()Tự nguyện từ bỏ CPU os. sched_setaffinity(pid , mặt nạ, /)Hạn chế quy trình với PID pid (hoặc quy trình hiện tại nếu bằng 0) đối với một bộ CPU. mặt nạ là một số nguyên có thể lặp lại đại diện cho tập hợp CPU mà quy trình sẽ bị hạn chế os. sched_getaffinity(pid , /)Trả về bộ CPU, quy trình có PID pid (hoặc quy trình hiện tại nếu bằng 0) bị hạn chế đối với Thông tin hệ thống khácos. confstr(tên , /)Trả về các giá trị cấu hình hệ thống có giá trị chuỗi. tên chỉ định giá trị cấu hình để truy xuất; . Một số nền tảng cũng xác định tên bổ sung. The names known to the host operating system are given as the keys of the if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"389 dictionary. Đối với các biến cấu hình không có trong ánh xạ đó, việc chuyển một số nguyên cho tên cũng được chấp nhận Nếu giá trị cấu hình được chỉ định theo tên không được xác định, thì ______25_______35 được trả về Nếu tên là một chuỗi và không được biết, được nâng lên. Nếu một giá trị cụ thể cho tên không được hệ thống máy chủ hỗ trợ, ngay cả khi nó được bao gồm trong if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"389, thì một lỗi sẽ xuất hiện với số lỗi Unix os. confstr_namesCác tên ánh xạ từ điển được chấp nhận bởi các giá trị số nguyên được xác định cho các tên đó bởi hệ điều hành máy chủ. Điều này có thể được sử dụng để xác định tập hợp các tên được hệ thống biết đến Unix os. số lượng cpu()Trả về số lượng CPU trong hệ thống. Trả về if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35 nếu không xác định Con số này không tương đương với số lượng CPU mà tiến trình hiện tại có thể sử dụng. Số lượng CPU có thể sử dụng có thể thu được bằng if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"397 New in version 3. 4 os. getloadavg()Trả về số lượng quy trình trong hàng đợi chạy hệ thống được tính trung bình trong 1, 5 và 15 phút qua hoặc tăng nếu không thể đạt được mức tải trung bình Unix os. sysconf(name , /)Trả về các giá trị cấu hình hệ thống có giá trị số nguyên. Nếu giá trị cấu hình được chỉ định theo tên không được xác định, thì ______3_______32 được trả về. Các nhận xét về tham số tên cũng được áp dụng ở đây; Unix os. sysconf_namesCác tên ánh xạ từ điển được chấp nhận bởi các giá trị số nguyên được xác định cho các tên đó bởi hệ điều hành máy chủ. Điều này có thể được sử dụng để xác định tập hợp các tên được hệ thống biết đến Unix Đã thay đổi trong phiên bản 3. 11. Thêm tên if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"403. Các giá trị dữ liệu sau đây được sử dụng để hỗ trợ các thao tác thao tác trên đường dẫn. Chúng được xác định cho tất cả các nền tảng Các hoạt động cấp cao hơn trên tên đường dẫn được xác định trong mô-đun os. sữa chuaChuỗi hằng số được hệ điều hành sử dụng để chỉ thư mục hiện tại. Đây là os.stat in os.supports_dir_fd14 cho Windows và POSIX. Cũng có sẵn thông quaos. xin lỗi Chuỗi hằng được hệ điều hành sử dụng để chỉ thư mục mẹ. Đây là os.stat in os.supports_dir_fd15 cho Windows và POSIX. Cũng có sẵn thông quaos. tháng 9 Ký tự được hệ điều hành sử dụng để phân tách các thành phần tên đường dẫn. Đây là if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"221 cho POSIX và if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"410 cho Windows. Lưu ý rằng biết điều này là không đủ để có thể phân tích cú pháp hoặc nối tên đường dẫn - sử dụng và - nhưng đôi khi nó hữu ích. Cũng có sẵn thông quaos. altsep Một ký tự thay thế được hệ điều hành sử dụng để phân tách các thành phần tên đường dẫn hoặc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"35 nếu chỉ tồn tại một ký tự phân tách. Điều này được đặt thành if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"221 trên các hệ thống Windows trong đó if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"416 là dấu gạch chéo ngược. Cũng có sẵn thông quaos. extsep Ký tự phân tách tên tệp cơ sở với phần mở rộng; . Cũng có sẵn thông qua os. pathsepKý tự thường được hệ điều hành sử dụng để phân tách các thành phần đường dẫn tìm kiếm (như trong if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"027), chẳng hạn như if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"422 cho POSIX hoặc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"423 cho Windows. Cũng có sẵn thông quaos. defpath Đường dẫn tìm kiếm mặc định được sử dụng bởi và nếu môi trường không có khóa if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"427. Cũng có sẵn thông quaos. dòng Chuỗi được sử dụng để phân tách (hay đúng hơn là chấm dứt) các dòng trên nền tảng hiện tại. Đây có thể là một ký tự, chẳng hạn như if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"429 cho POSIX hoặc nhiều ký tự, chẳng hạn như if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"430 cho Windows. Không sử dụng hệ điều hành. linesep làm dấu kết thúc dòng khi ghi tệp được mở ở chế độ văn bản (mặc định); os. devnull Đường dẫn tệp của thiết bị null. For example. if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"432 cho POSIX, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"433 cho Windows. Cũng có sẵn thông quaos. RTLD_LAZYos. RTLD_NOWos. RTLD_GLOBALos. RTLD_LOCALos. RTLD_NODELETEos. RTLD_NOLOADos. RTLD_DEEPBIND Cờ để sử dụng với các chức năng và. Xem trang thủ công Unix dlopen(3) để biết ý nghĩa của các cờ khác nhau New in version 3. 3 Số ngẫu nhiênos. getrandom(kích thước , cờ=0)Lên đến kích thước byte ngẫu nhiên. Hàm có thể trả về ít byte hơn yêu cầu Các byte này có thể được sử dụng để khởi tạo các trình tạo số ngẫu nhiên trong không gian người dùng hoặc cho các mục đích mã hóa if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"437 dựa vào entropy được thu thập từ trình điều khiển thiết bị và các nguồn tiếng ồn môi trường khác. Unnecessarily reading large quantities of data will have a negative impact on other users of the if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"438 and if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"439 devices Đối số flags là một mặt nạ bit có thể chứa 0 hoặc nhiều giá trị sau ORed cùng nhau. và Xem thêm trang thủ công Linux getrandom() Linux >= 3. 17 Mới trong phiên bản 3. 6 os. độ ngẫu nhiên(kích thước , /)Trả về một chuỗi byte ngẫu nhiên có kích thước phù hợp cho việc sử dụng mật mã Hàm này trả về các byte ngẫu nhiên từ nguồn ngẫu nhiên dành riêng cho hệ điều hành. Dữ liệu được trả về phải đủ khó đoán đối với các ứng dụng mật mã, mặc dù chất lượng chính xác của nó phụ thuộc vào việc triển khai hệ điều hành Trên Linux, nếu tòa nhà chọc trời if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"437 có sẵn, nó được sử dụng ở chế độ chặn. chặn cho đến khi nhóm entropy urandom của hệ thống được khởi tạo (128 bit entropy được kernel thu thập). Xem PEP 524 để biết lý do. Trên Linux, chức năng này có thể được sử dụng để lấy các byte ngẫu nhiên ở chế độ không chặn (sử dụng cờ) hoặc để thăm dò cho đến khi nhóm entropy urandom của hệ thống được khởi tạo Trên hệ thống giống Unix, các byte ngẫu nhiên được đọc từ thiết bị if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"439. Nếu thiết bị if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"439 không có sẵn hoặc không thể đọc được, ngoại lệ sẽ được đưa ra Trên Windows, nó sẽ sử dụng if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"448 See also The module provides higher level functions. Để biết giao diện dễ sử dụng cho trình tạo số ngẫu nhiên do nền tảng của bạn cung cấp, vui lòng xem Đã thay đổi trong phiên bản 3. 6. 0. Trên Linux, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"437 hiện được sử dụng ở chế độ chặn để tăng tính bảo mật. Đã thay đổi trong phiên bản 3. 5. 2. Trên Linux, nếu khối syscall if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"437 (nhóm entropy urandom chưa được khởi tạo), hãy quay lại đọc if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"439. Đã thay đổi trong phiên bản 3. 5. Trên Linux 3. 17 trở lên, syscall if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"437 hiện được sử dụng khi khả dụng. Trên OpenBSD 5. 6 và mới hơn, chức năng C if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"455 hiện được sử dụng. Các chức năng này tránh việc sử dụng bộ mô tả tệp nội bộ. Đã thay đổi trong phiên bản 3. 11. Trên Windows, if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"448 được sử dụng thay vì if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"457 không được dùng nữa. os. GRND_NONBLOCK Theo mặc định, khi đọc từ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"438, nó sẽ chặn nếu không có byte ngẫu nhiên nào và khi đọc từ if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"439, nó sẽ chặn nếu nhóm entropy chưa được khởi tạo Nếu cờ được đặt, thì không chặn trong những trường hợp này mà thay vào đó tăng ngay lập tức Mới trong phiên bản 3. 6 os. GRND_RANDOMNếu bit này được đặt, thì các byte ngẫu nhiên được lấy từ nhóm if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"438 thay vì nhóm if os.access("myfile", os.R_OK): with open("myfile") as fp: return fp.read() return "some default data"439 Đoạn mã nào trả về đường dẫn thư mục của plugin?Sử dụng hàm plugin_dir_path() sẽ trả về đường dẫn của thư mục tệp từ nơi nó được gọi.
__ dir __ trong WordPress là gì?Có thể sử dụng __DIR__ để lấy thư mục làm việc của mã hiện tại . Nó đã được giới thiệu trong PHP bắt đầu từ phiên bản 5. 3. Nó tương tự như sử dụng dirname(__FILE__). Thông thường, nó được sử dụng để bao gồm các tệp khác có trong một tệp được bao gồm.
Đoạn mã nào sau đây sẽ trả về thư mục plugin hiện tại trong WordPress?echo PluginUrl(); sẽ trả về URL plugin hiện tại.
Thư mục của plugin là gì?Cái gì. Thư mục plugin. Danh mục trình cắm WordPress là nguồn chính thức của các trình cắm WordPress miễn phí . Nó còn được gọi là kho lưu trữ plugin WordPress hoặc repo. Plugin giúp dễ dàng thêm các tính năng mới vào trang web của bạn và thư mục plugin giúp dễ dàng cài đặt chúng. |