How do I read and append to a text file in one pass?
问题 I want to check if a string is inside a text file and then append that string if it's not there. I know I can probably do that by creating two separate with methods, one for reading and another for appending, but is it possible to read and append inside the same with method? The closest I came up with is this: with open("file.txt","r+") as file: content=file.read() print("aaa" in content) file.seek(len(content)) file.write("\nccccc") My file.txt: aaaaa bbbbb When I run the code for the first