Skip to content

Latest commit

 

History

History
49 lines (35 loc) · 1.05 KB

24-Write-to-a-File-in-Python.md

File metadata and controls

49 lines (35 loc) · 1.05 KB

We can also create files from within our Python code.

Creating a Text File With Python

We'll need to use the open method to do this. This will create a new file outside of our Python code and put whatever contents we specify.

f = open('cars.txt', 'w')
cars = ['chevy', 'tesla', 'ford']
  for car in cars:
    f.write(car + '\n')

Using the close Method

Again, we should use the close method to close the file as soon as our code finishes executing.

f.close()

Using with Blocks

We can also use with blocks for this.

with open('cars.txt', 'a') as f:
  cars = ['chevy', 'vw', 'mazda']
  for car in cars:
    f.write(car +'\n)

Creating JSON Files

We can also create an external JSON file in our Python code. Start by creating a JSON object in your code.

cars = [
  {"make": "chevy"},
  {"make": "tesla"},
  {"make": "porsche"}
]

import json
with open('cars.json', 'w') as f:
  json.dump(cars, f)