English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes two simple ways to implement secure switch files in Python. Share with everyone for reference, as follows:
The following code is implemented by Python3.3Test.
mode1:
try: file = open('config.ini', 'w') print("It's a text file", file=file) except IOError as err: print('File error: ') + str(err)) finally: if 'file' in locals(): file.close()
mode2:
try: with open('config.txt', 'w') as file: print("It's a text file", file=file) except IOError as err: print('File error: ') + str(err))
Readers who are interested in more content related to Python can check the special topic of this site: 'Summary of Python File and Directory Operation Skills', 'Summary of Python Text File Operation Skills', 'Summary of Python URL Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', and 'Classic Tutorial of Python入门与进阶'.
I hope the content described in this article will be helpful to everyone in Python program design.
Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users, this website does not own the copyright, does not edit manually, and does not bear relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.