what does with open do in this situation [duplicate]

2024/11/7 20:42:37
sentence = "ASK NOT WHAT YOUR COUNTRY CAN DO FOR YOU ASK WHAT YOU CAN DO FOR YOUR COUNTRY"
s=sentence.split()
positions = [s.index(x)+1 for x in s]
print(sentence)
print(positions)
with open('task_2.json', 'w') as f:f.write(str(positions))f.write(str(sentence))
f.close()

In this task I must develop a program that identifies individual words in a sentence, stores these in a list and replaces each word in the original sentence with the position of the word in the list. It must then compress the positions and send it to a file. I have managed to complete all of the above but im struggling to understand the "with open" part. I have tried just using "open" and the code comes up with a syntax error. So if anyone could explain to me what the keyword "with" does in this situation it would be much appreciated.

Answer

The with open('task_2.json', 'w') as f: closes the file after the indented code runs. This means that you don't need the f.close() line. Using a with statement in python is just a safer way to work with files.

https://en.xdnf.cn/q/120697.html

Related Q&A

How to perform HTTP GET operation in Python? [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Python Error TypeError: cannot concatenate str and float objects [duplicate]

This question already has answers here:Making a string out of a string and an integer in Python [duplicate](5 answers)Closed 7 years ago.I am new with Python programming. I keep getting the below error…

Countif function in python

enter image description here In excel file i can do countif funtion like attached picture but How can i do this countif function in Python Pandas,please help me by providing the code

How do i implement these algorithms below

Alogrithm 1:Get a list of numbers L1, L2, L3....LN as argumentAssume L1 is the largest, Largest = L1Take next number Li from the list and do the followingIf Largest is less than LiLargest = LiIf Li is …

How to run a shell script once a day?

I am trying to run this particular shell script only one time, daily. Heres my code for runLucene.py:#!/usr/bin/env pythonimport os from extras.download_datos_desambiguar import news_Lucenex=datetime.t…

How to fix Error: Please select a valid Python interpreter in Pycharm?

Error:Error: Please select a valid Python interpreterScreenshot:How to fix this?

Tuples conversion into JSON with python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 8 years ago.Improve…

Python continue with while [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

Create MySQL database with python [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Basic Python repetition exercise

I was trying to learn Python when I came upon this question. I am not asking for you to answer the question for me, I just need some help. Note: I am only allowed to use loops and if statements etc. No…