Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality.
Bàn luận
This method returns a file descriptor for newly open file. The returned file descriptor is non-inheritable.
Mô -đun HĐH trong Python cung cấp các chức năng để tương tác với hệ điều hành. Hệ điều hành thuộc các mô -đun tiện ích tiêu chuẩn Python. Mô -đun này cung cấp một cách di động để sử dụng chức năng phụ thuộc hệ điều hành. os.open[path, flags, mode = 0o777, *, dir_fd = None]
Phương thức
os.open[]
trong Python được sử dụng để mở một đường dẫn tệp được chỉ định và đặt các cờ khác nhau theo các cờ được chỉ định và chế độ của nó theo chế độ được chỉ định. Phương thức này trả về một bộ mô tả tệp cho tệp mới mở. Bộ mô tả tệp được trả về là không thể không thể cưỡng lại được.
Path: A path-like object representing the file system path. This is the file path to be opened.
A path-like object is a string or bytes object which represents a path.
flags: This parameter specify the flags to be set for newly opened file.
mode [optional]: A numeric value representing the mode of the newly opened file. The default value of this parameter is 0o777 [octal].
dir_fd [optional]: A file descriptor referring to a directory.Cú pháp: os.open [đường dẫn, cờ, mode = 0o777, *, dir_fd = none] This method returns a file descriptor for newly opened file.
Tham số: Đường dẫn: Một đối tượng giống như đường dẫn biểu thị đường dẫn hệ thống tệp. Đây là đường dẫn tệp được mở. Đối tượng giống như đường dẫn là một chuỗi hoặc đối tượng byte đại diện cho một đường dẫn. Chế độ của tệp mới được mở. Giá trị mặc định của tham số này là 0o777 [octal] .dir_fd [tùy chọn]: một mô tả tệp đề cập đến một thư mục. Use of os.open[]
method to open a file path
Loại trả về: Phương thức này trả về một bộ mô tả tệp cho tệp mới được mở.
Mã: Sử dụng phương thức os.open[]
để mở đường dẫn tệp
import
os
path
=
os.open[file, flags[, mode]];0
os.open[file, flags[, mode]];1
=
os.open[file, flags[, mode]];3
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"2
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"3
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"4
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"5
os.open[file, flags[, mode]];4
=
os.open[file, flags[, mode]];6
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"9
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"6
Closed the file successfully!!1
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"2
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"3
Closed the file successfully!!4
Closed the file successfully!!5
os.open[file, flags[, mode]];7
=
os.open[file, flags[, mode]];9
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"0
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"1
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"6
=
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"8
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"2
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"3
os.open[]
6#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"5
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"2
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"3
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"6
os.open[]
1os.open[]
2
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"2
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"3
os.open[]
5#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"5
Output:
File path opened successfully. String written to the file descriptor. String read from file descriptor: GeeksforGeeks: A computer science portal for geeks. File descriptor closed successfully.
Closed the file successfully!!
6Closed the file successfully!!
7Closed the file successfully!!
8Closed the file successfully!!
7#!/usr/bin/python
import os, sys
# Open a file
fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ]
# Write one string
os.write[fd, "This is test"]
# Close opened file
os.close[ fd ]
print "Closed the file successfully!!"
5 //docs.python.org/3/library/os.html#os.open
Sự mô tả
Phương thức Python Open [] mở tệp tệp và đặt các cờ khác nhau theo cờ và có thể chế độ của nó theo chế độ. Chế độ mặc định là 0777 [octal] và giá trị UMASK hiện tại được che dấu trước tiên.open[] opens the file file and set various flags according to flags and possibly its mode according to mode.The default mode is 0777 [octal], and the current umask value is first masked out.
Cú pháp
Sau đây là Cú pháp cho phương thức Open [] -open[] method −
os.open[file, flags[, mode]];
Thông số
Tệp - Tên tệp sẽ được mở. − File name to be opened.
Cờ - Các hằng số sau là các tùy chọn cho các cờ. Chúng có thể được kết hợp bằng cách sử dụng bitwise hoặc toán tử |. Một số trong số họ không có sẵn trên tất cả các nền tảng. − The following constants are options for the flags. They can be combined using the bitwise OR operator |. Some of them are not available on all platforms.
os.o_rdonly - mở để chỉ đọc − open for reading only
os.o_wronly - chỉ mở để viết − open for writing only
OS.O_RDWR - Mở để đọc và viết − open for reading and writing
os.o_nonblock - không chặn trên mở − do not block on open
os.o_append - nối vào mỗi lần ghi − append on each write
os.o_creat - tạo tệp nếu nó không tồn tại − create file if it does not exist
os.o_trunc - kích thước cắt ngắn thành 0 − truncate size to 0
os.o_excl - lỗi nếu tạo và tệp tồn tại − error if create and file exists
os.o_shlock - về mặt nguyên tử có được khóa chia sẻ − atomically obtain a shared lock
os.o_exlock - về mặt nguyên tử có được khóa độc quyền − atomically obtain an exclusive lock
OS.O_Direct - loại bỏ hoặc giảm hiệu ứng bộ đệm − eliminate or reduce cache effects
os.o_fsync - ghi đồng bộ − synchronous writes
OS.O_NOFOWLOWL - Không theo dõi Symlinks − do not follow symlinks
Chế độ - Công việc này theo cách tương tự như nó hoạt động cho phương thức chmod []. − This work in similar way as it works for chmod[] method.
Giá trị trả về
Phương thức này trả về bộ mô tả tệp cho tệp mới được mở.
Thí dụ
Ví dụ sau đây cho thấy việc sử dụng phương thức Open [].
#!/usr/bin/python import os, sys # Open a file fd = os.open[ "foo.txt", os.O_RDWR|os.O_CREAT ] # Write one string os.write[fd, "This is test"] # Close opened file os.close[ fd ] print "Closed the file successfully!!"
Điều này sẽ tạo tệp foo.txt đã cho và sau đó sẽ viết nội dung đã cho vào tệp đó và sẽ tạo ra kết quả sau -
Closed the file successfully!!
python_files_io.htm