avec syntaxe (Python)

L'utilisation de la syntaxe with est pratique car vous pouvez omettre close (), que vous oubliez, et cela améliore également la lisibilité.

Utilisation ```plaintext avec fichier lu comme variable: ~~~~ ```

Exemple Pour écrire "Hello, world!" À text.txt, ce sera comme suit.

python


#avec instruction utilisée
with open("text.txt", 'w') as text:
    text.write("Hello, world!")

Si vous essayez d'écrire ceci sans l'instruction with, ce sera comme suit.

python


#avec instruction non utilisée
text = None
try:
  text = open("text.txt", 'w')
  try:
    text.write("Hello, world!")
  except:
    raise
finally:
  if text:
    text.close()

Recommended Posts

avec syntaxe (Python)
FizzBuzz en Python3
Grattage avec Python
Statistiques avec python
Grattage avec Python
Twilio avec Python
Intégrer avec Python
Jouez avec 2016-Python
AES256 avec python
python commence par ()
Bingo avec python
Zundokokiyoshi avec python
Syntaxe de contrôle de la syntaxe Python
Excel avec Python
Micro-ordinateur avec Python
Cast avec python
Communication série avec Python
Django 1.11 a démarré avec Python3.6
Jugement des nombres premiers avec Python
Python avec eclipse + PyDev.
Communication de socket avec Python
Analyse de données avec python 2
Grattage en Python (préparation)
Essayez de gratter avec Python.
Apprendre Python avec ChemTHEATER 03
"Orienté objet" appris avec python
Exécutez Python avec VBA
Manipuler yaml avec python
Résolvez AtCoder 167 avec python
Communication série avec python
[Python] Utiliser JSON avec Python
Apprendre Python avec ChemTHEATER 05-1
Apprenez Python avec ChemTHEATER
Exécutez prepDE.py avec python3
1.1 Premiers pas avec Python
Collecter des tweets avec Python
Binarisation avec OpenCV / Python
3. 3. Programmation IA avec Python
Méthode Kernel avec Python
Non bloquant avec Python + uWSGI
Grattage avec Python + PhantomJS
Publier des tweets avec python
Conduisez WebDriver avec python
Utiliser mecab avec Python 3
[Python] Redirection avec CGIHTTPServer
Analyse vocale par python
Pensez à yaml avec python
Premiers pas avec Python
Utiliser DynamoDB avec Python
Getter Zundko avec python
Gérez Excel avec python
Loi d'Ohm avec Python
Jugement des nombres premiers avec python
Résoudre des maths avec Python
Python à partir de Windows 7
Carte thermique par Python + matplotlib
Programmation Python avec Atom
Apprendre Python avec ChemTHEATER 02
Utilisez Python 3.8 avec Anaconda
Programmation compétitive avec python
Manipuler rabbimq avec python