How do you edit a text file in Python?
Answer
Use file. readlines() to edit a file
Answer
Use file. readlines() to edit a file
- my_file = open(“data.txt”)
- string_list = my_file. readlines() …
- my_file. close()
- print(string_list)
- string_list[1] = “Edit the list of strings as desiredn”
- my_file = open(“data.txt”, “w”)
- new_file_contents = “”. join(string_list) …
- my_file. write(new_file_contents)