python append a string to a file
To append a string to a file in Python, you can use the open() function with the mode parameter set to 'a' (for append). Then, you can use the write() method to add the desired string to the file.
Here's an example:
# Open the file in append mode
file = open('file.txt', 'a')
# String to be appended
string_to_append = "This is a new line."
# Append the string to the file
file.write(string_to_append)
# Close the file
file.close()
In this example, the string "This is a new line." will be appended to the file named 'file.txt'. Make sure to replace 'file.txt' with the actual path and name of your file
原文地址: http://www.cveoy.top/t/topic/i1zL 著作权归作者所有。请勿转载和采集!