Hướng dẫn what is used to store temporary data in python? - những gì được sử dụng để lưu trữ dữ liệu tạm thời trong python?
Tôi đồng ý với nhận xét của Zvone. Để biết giải pháp tốt nhất, chúng tôi sẽ cần thêm thông tin. Show Sau đây sẽ là một giải pháp mạnh mẽ và có thể định cấu hình.
Tôi đã chuyển mã của bạn vào một số chức năng. Với biến Backup_interval biến, có thể kiểm soát tần suất sao lưu được ghi vào đĩa. Ghi chú bổ sung:
EDIT: Tôi vừa xem xét lại trường hợp sử dụng của bạn. Vấn đề chính ở đây là: Khôi phục dữ liệu, ngay cả khi chương trình bị gián đoạn bất cứ lúc nào (do gián đoạn điện hoặc bất cứ điều gì). Giải pháp đầu tiên của tôi chỉ viết dữ liệu cho đĩa (giải quyết một phần của vấn đề), nhưng nó vẫn có thể xảy ra, chương trình này đã gặp sự cố khi viết vào đĩa. Trong trường hợp đó, tệp có thể sẽ bị hỏng và dữ liệu bị mất. Tôi đã điều chỉnh hàm write_backup_data (), để nó ghi vào một tệp tạm thời trước và sau đó thay thế tệp cũ. Vì vậy, bây giờ, ngay cả khi rất nhiều dữ liệu phải được ghi vào đĩa và sự cố xảy ra ở đó, tệp sao lưu trước đó vẫn sẽ có sẵn.: I just reconsidered your use case. The main problem here is: Restoring data, even if the program gets interrupted at any time (due to power interruption or whatever). My first solution just wrote the data to disk (which solves part of the problem), but it could still happen, that the program crashes the moment when writing to disk. In that case the file would probably be corrupt and the data lost. I adapted the function write_backup_data(), so that it writes to a temporary file first and then replaces the old file. So now, even if a lot of data has to be written to disk and the crash happens there, the previous backup file would still be available. Mã nguồn: lib/tempfile.py Lib/tempfile.py Mô -đun này tạo ra các tập tin và thư mục tạm thời. Nó hoạt động trên tất cả các nền tảng được hỗ trợ. Tất cả các chức năng và trình xây dựng có thể gọi người dùng đều có các đối số bổ sung cho phép kiểm soát trực tiếp vị trí và tên của các tệp và thư mục tạm thời. Tên các tệp được sử dụng bởi mô -đun này bao gồm một chuỗi các ký tự ngẫu nhiên cho phép các tệp đó được tạo an toàn trong các thư mục tạm thời được chia sẻ. Để duy trì khả năng tương thích ngược, thứ tự đối số có phần kỳ lạ; Nên sử dụng các đối số từ khóa để rõ ràng. Mô-đun xác định các mục có thể gọi người dùng sau: tempfile.temdyfile (mode = 'w+b', buffering = -1, mã hóa = không, newline = none, hậu tố = không, tiền tố = khôngTemporaryFile(mode='w+b', buffering=- 1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)¶Trả về một đối tượng giống như tệp có thể được sử dụng làm khu vực lưu trữ tạm thời. Tệp được tạo an toàn, sử dụng các quy tắc tương tự như Đối tượng kết quả có thể được sử dụng làm trình quản lý ngữ cảnh (xem ví dụ). Khi hoàn thành bối cảnh hoặc phá hủy đối tượng tệp, tệp tạm thời sẽ bị xóa khỏi hệ thống tập tin.Examples). On completion of the context or destruction of the file object the temporary file will be removed from the filesystem. Tham số chế độ mặc định là >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed0 để tệp được tạo và ghi mà không bị đóng. Chế độ nhị phân được sử dụng để nó hoạt động liên tục trên tất cả các nền tảng mà không liên quan đến dữ liệu được lưu trữ. Bộ đệm, mã hóa, lỗi và dòng mới được hiểu là đối với >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed1. Các tham số DIR, tiền tố và hậu tố có cùng ý nghĩa và mặc định như với Đối tượng được trả về là một đối tượng tệp thực trên các nền tảng POSIX. Trên các nền tảng khác, nó là một đối tượng giống như tệp có thuộc tính >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed3 là đối tượng tệp thực tế. Cờ >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed4 được sử dụng nếu nó có sẵn và hoạt động (đặc trưng của Linux, yêu cầu nhân Linux 3.11 trở lên). Trên các nền tảng không phải là POSIX hay Cygwin, TemplayFile là bí danh của tên được đặt tên. Tăng một sự kiện kiểm toán >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed5 với đối số >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6.auditing event >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed5 with argument >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6. Đã thay đổi trong phiên bản 3.5: cờ >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed4 flag is now used if available. Đã thay đổi trong phiên bản 3.8: Đã thêm tham số lỗi.Added errors parameter. Tempfile.namedTeMdyFile (mode = 'w+b', buffering = -1, expoding = none, newline = noneNamedTemporaryFile(mode='w+b', buffering=- 1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)¶Hàm này hoạt động chính xác như >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed8, ngoại trừ tệp được đảm bảo có tên hiển thị trong hệ thống tệp (trên Unix, mục nhập thư mục không được liên kết). Tên đó có thể được lấy từ thuộc tính >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed9 của đối tượng giống như tệp được trả về. Liệu tên có thể được sử dụng để mở tệp lần thứ hai hay không, trong khi tệp tạm thời được đặt tên vẫn còn mở, thay đổi giữa các nền tảng (nó có thể được sử dụng trên Unix; nó không thể trên Windows). Nếu xóa là đúng (mặc định), tệp sẽ bị xóa ngay khi đóng. Đối tượng được trả về luôn là một đối tượng giống như tệp có thuộc tính >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed3 là đối tượng tệp thực cơ bản. Đối tượng giống như tệp này có thể được sử dụng trong câu lệnh >>> f = NamedTemporaryFile(delete=False) >>> f.name '/tmp/tmptjujjt' >>> f.write(b"Hello World!\n") 13 >>> f.close() >>> os.unlink(f.name) >>> os.path.exists(f.name) False1, giống như một tệp thông thường. Trên POSIX (chỉ), một quy trình bị chấm dứt đột ngột với Sigkill không thể tự động xóa bất kỳ tên gọi nào mà nó tạo ra. Tăng một sự kiện kiểm toán >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed5 với đối số >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6.auditing event >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed5 with argument >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6. Đã thay đổi trong phiên bản 3.8: Đã thêm tham số lỗi.Added errors parameter. Tempfile.namedTeMdyFile (mode = 'w+b', buffering = -1, expoding = none, newline = none tempfile.SpooledTemporaryFile(max_size=0, mode='w+b', buffering=- 1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, *, errors=None)¶Hàm này hoạt động chính xác như >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed8, ngoại trừ tệp được đảm bảo có tên hiển thị trong hệ thống tệp (trên Unix, mục nhập thư mục không được liên kết). Tên đó có thể được lấy từ thuộc tính >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed9 của đối tượng giống như tệp được trả về. Liệu tên có thể được sử dụng để mở tệp lần thứ hai hay không, trong khi tệp tạm thời được đặt tên vẫn còn mở, thay đổi giữa các nền tảng (nó có thể được sử dụng trên Unix; nó không thể trên Windows). Nếu xóa là đúng (mặc định), tệp sẽ bị xóa ngay khi đóng. Đối tượng được trả về luôn là một đối tượng giống như tệp có thuộc tính >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed3 là đối tượng tệp thực cơ bản. Đối tượng giống như tệp này có thể được sử dụng trong câu lệnh >>> f = NamedTemporaryFile(delete=False) >>> f.name '/tmp/tmptjujjt' >>> f.write(b"Hello World!\n") 13 >>> f.close() >>> os.unlink(f.name) >>> os.path.exists(f.name) False1, giống như một tệp thông thường. Trên POSIX (chỉ), một quy trình bị chấm dứt đột ngột với Sigkill không thể tự động xóa bất kỳ tên gọi nào mà nó tạo ra. classtempfile.spooledtemdyfile (max_size = 0, mode = 'w+b', buffering = -1, expoding = none, newline = none, hậu tố = không, tiền tố = không, dir = none, Lớp này hoạt động chính xác như Đã thay đổi trong phiên bản 3.8: Đã thêm tham số lỗi.Added errors parameter. Tempfile.namedTeMdyFile (mode = 'w+b', buffering = -1, expoding = none, newline = noneFully implements the >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed8, ngoại trừ tệp được đảm bảo có tên hiển thị trong hệ thống tệp (trên Unix, mục nhập thư mục không được liên kết). Tên đó có thể được lấy từ thuộc tính >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed9 của đối tượng giống như tệp được trả về. Liệu tên có thể được sử dụng để mở tệp lần thứ hai hay không, trong khi tệp tạm thời được đặt tên vẫn còn mở, thay đổi giữa các nền tảng (nó có thể được sử dụng trên Unix; nó không thể trên Windows). Nếu xóa là đúng (mặc định), tệp sẽ bị xóa ngay khi đóng. Đối tượng được trả về luôn là một đối tượng giống như tệp có thuộc tính >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed3 là đối tượng tệp thực cơ bản. Đối tượng giống như tệp này có thể được sử dụng trong câu lệnh >>> f = NamedTemporaryFile(delete=False) >>> f.name '/tmp/tmptjujjt' >>> f.write(b"Hello World!\n") 13 >>> f.close() >>> os.unlink(f.name) >>> os.path.exists(f.name) False1, giống như một tệp thông thường.tempfile.TemporaryDirectory(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)¶ Trên POSIX (chỉ), một quy trình bị chấm dứt đột ngột với Sigkill không thể tự động xóa bất kỳ tên gọi nào mà nó tạo ra.Examples). On completion of the context or destruction of the temporary directory object, the newly created temporary directory and all its contents are removed from the filesystem. classtempfile.spooledtemdyfile (max_size = 0, mode = 'w+b', buffering = -1, expoding = none, newline = none, hậu tố = không, tiền tố = không, dir = none, Lớp này hoạt động chính xác như >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed8, ngoại trừ dữ liệu được trình bày trong bộ nhớ cho đến khi kích thước tệp vượt quá MAX_SIZE hoặc cho đến khi phương thức tệp ____ ____25 được gọi, tại thời điểm đó, nội dung được ghi vào đĩa và hoạt động tiến hành như với >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed8. Tăng một sự kiện kiểm toán >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6.auditing event NamedTemporaryFile 4 with argument >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6. Mới trong phiên bản 3.2. Đã thay đổi trong phiên bản 3.10: Đã thêm tham số bỏ qua_cleanup_errors.Added ignore_cleanup_errors parameter. tempfile.mkstemp (hậu tố = không, tiền tố = none, dir = none, text = false) ¶mkstemp(suffix=None, prefix=None, dir=None, text=False)¶Tạo một tập tin tạm thời theo cách an toàn nhất có thể. Không có điều kiện đua trong việc tạo tệp, giả sử rằng nền tảng thực hiện đúng cờ Không giống như >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed8, người dùng của mkstemp() chịu trách nhiệm xóa tệp tạm thời khi thực hiện với nó.Nếu hậu tố không phải là Nếu tiền tố không phải là Nếu DIR không phải là Nếu bất kỳ hậu tố, tiền tố và dir không phải là Nếu văn bản được chỉ định và đúng, tệp được mở ở chế độ văn bản. Mặt khác, (mặc định) tệp được mở ở chế độ nhị phân.
Tăng một sự kiện kiểm toán >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed5 với đối số >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6.auditing event >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed5 with argument >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6. Đã thay đổi trong phiên bản 3.5: Hậu tố, tiền tố và dir hiện có thể được cung cấp theo byte để có được giá trị trả về byte. Trước đó, chỉ có STR được phép. Hậu tố và tiền tố hiện chấp nhận và mặc định là Đã thay đổi trong phiên bản 3.6: Tham số Dir hiện chấp nhận một đối tượng giống như đường dẫn.The dir parameter now accepts a path-like object. tempfile.mkdtemp (hậu tố = không, tiền tố = không, dir = none) ¶mkdtemp(suffix=None, prefix=None, dir=None)¶Tạo một thư mục tạm thời theo cách an toàn nhất có thể. Không có điều kiện chủng tộc trong việc tạo thư mục. Thư mục có thể đọc được, có thể ghi và chỉ có thể tìm kiếm bằng ID người dùng tạo. Người dùng của Tiền tố, hậu tố và đối số DIR giống như đối với
Tăng một sự kiện kiểm toán >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6.auditing event NamedTemporaryFile 4 with argument >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed6. Đã thay đổi trong phiên bản 3.5: Hậu tố, tiền tố và dir hiện có thể được cung cấp theo byte để có được giá trị trả về byte. Trước đó, chỉ có STR được phép. Hậu tố và tiền tố hiện chấp nhận và mặc định là Đã thay đổi trong phiên bản 3.6: Tham số Dir hiện chấp nhận một đối tượng giống như đường dẫn.The dir parameter now accepts a path-like object. tempfile.mkdtemp (hậu tố = không, tiền tố = không, dir = none) ¶gettempdir()¶Tạo một thư mục tạm thời theo cách an toàn nhất có thể. Không có điều kiện chủng tộc trong việc tạo thư mục. Thư mục có thể đọc được, có thể ghi và chỉ có thể tìm kiếm bằng ID người dùng tạo. Người dùng của
Thư mục được đặt tên bởi biến môi trường Vị trí cụ thể của nền tảng:Always returns a str. Previously it would return any mkstemp() 3, mkstemp() 4, mkstemp() 5 và mkstemp() 6, theo thứ tự đó.gettempdirb()¶Trên tất cả các nền tảng khác, các thư mục Là một phương sách cuối cùng, thư mục làm việc hiện tại. Kết quả của tìm kiếm này được lưu trữ, xem mô tả củamkdtemp() 0 bên dưới.gettempprefix()¶Đã thay đổi trong phiên bản 3.10: Luôn trả về STR. Trước đây, nó sẽ trả về bất kỳ giá trị Giống như Mới trong phiên bản 3.5. Mô -đun sử dụng biến toàn cầu để lưu trữ tên của thư mục được sử dụng cho các tệp tạm thời được trả về bởi Khi được đặt thành một giá trị khác với Nếu Ghi chú Hãy coi chừng nếu bạn đặt Ví dụ;Dưới đây là một số ví dụ về cách sử dụng điển hình của mô -đun >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed Chức năng và biến không dùng nữaMột cách lịch sử để tạo các tệp tạm thời là trước tiên tạo tên tệp với hàm Không dùng nữa kể từ phiên bản 2.3: sử dụng Trả về một tên đường dẫn tuyệt đối của một tệp không tồn tại tại thời điểm cuộc gọi được thực hiện. Tiền tố, hậu tố và các đối số DIR tương tự như của >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed03 và >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed04 không được hỗ trợ. Cảnh báo Việc sử dụng chức năng này có thể giới thiệu một lỗ hổng bảo mật trong chương trình của bạn. Vào thời điểm bạn có thể làm bất cứ điều gì với tên tệp mà nó trả về, người khác có thể đã đánh bại bạn với cú đấm. >>> import tempfile # create a temporary file and write some data to it >>> fp = tempfile.TemporaryFile() >>> fp.write(b'Hello world!') # read data from file >>> fp.seek(0) >>> fp.read() b'Hello world!' # close the file, it will be removed >>> fp.close() # create a temporary file using a context manager >>> with tempfile.TemporaryFile() as fp: ... fp.write(b'Hello world!') ... fp.seek(0) ... fp.read() b'Hello world!' >>> # file is now closed and removed # create a temporary directory using the context manager >>> with tempfile.TemporaryDirectory() as tmpdirname: ... print('created temporary directory', tmpdirname) >>> # directory and contents have been removed06, chuyển cho nó tham số ____107: >>> f = NamedTemporaryFile(delete=False) >>> f.name '/tmp/tmptjujjt' >>> f.write(b"Hello World!\n") 13 >>> f.close() >>> os.unlink(f.name) >>> os.path.exists(f.name) False Làm thế nào để Python lưu trữ dữ liệu tạm thời?Để đạt được điều này, chúng ta có thể sử dụng hàm tạm thời () ... Đầu tiên, chúng ta phải nhập tempfile sau đó tệp được tạo bằng hàm tạm thờifile () .. Tệp được mở trong chế độ W+B (cả đọc và ghi vào tệp mở) theo mặc định .. Hàm này tạo một tệp tạm thời trong thư mục tạm thời và trả về một đối tượng tệp .. Python lưu trữ các tệp tạm thời ở đâu?Hàm này trả về tên của thư mục để lưu trữ các tệp tạm thời.Tên này thường được lấy từ biến môi trường tempdir.Trên nền tảng Windows, thông thường là người dùng/appdata/local/temp hoặc windowsdir/temp hoặc systemdrive/temp.Trên Linux, nó thường là /TMP.user/AppData/Local/Temp or windowsdir/temp or systemdrive/temp. On linux it normally is /tmp.
Những gì được sử dụng để lưu trữ dữ liệu trong Python?Sử dụng đối tượng tệp tích hợp của Python, có thể ghi dữ liệu chuỗi vào tệp đĩa và đọc từ nó.Thư viện tiêu chuẩn của Python, cung cấp các mô -đun để lưu trữ và truy xuất dữ liệu được tuần tự hóa trong các cấu trúc dữ liệu khác nhau như JSON và XML.DB-API của Python cung cấp một cách tương tác tiêu chuẩn với cơ sở dữ liệu quan hệ.Python's standard library, provides modules to store and retrieve serialized data in various data structures such as JSON and XML. Python's DB-API provides a standard way of interacting with relational databases.
Thư mục tạm thời Python là gì?Tạm thờifile, được đặt tênTemdyfile, tạm thời và tạm thời là giao diện cấp cao cung cấp việc dọn dẹp tự động và có thể được sử dụng làm người quản lý bối cảnh.mkstemp () và mkdtemp () là các hàm cấp thấp hơn yêu cầu dọn dẹp thủ công.high-level interfaces which provide automatic cleanup and can be used as context managers. mkstemp() and mkdtemp() are lower-level functions which require manual cleanup. |