Python simply implements two ways of security switch files

  • 2020-05-12 02:47:50
  • OfStack

This article illustrates two ways that Python can simply implement a secure switch file. I will share it with you for your reference as follows:

The following code was tested by Python3.3.

Method 1:


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()

Method 2:


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))

More about Python related topics: interested readers to view this site "Python file and directory skills summary", "Python skills summary text file", "Python URL skills summary", "Python pictures skills summary", "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using skills summary", "Python string skills summary" and "Python introductory and advanced tutorial"

I hope this article has been helpful to you in Python programming.


Related articles: