Skip to content Skip to sidebar Skip to footer

How To Append Data To Text File In Python 2.7.11?

Could any one show me how i can add hyperlinks to new line in text file? If there is already data in first line of text file i want the data get inserted in the next empty line. I

Solution 1:

Take a look at the python docs.

You can use the with open statement to open the file.

withopen(filename, 'a') as f:
    f.write(text)

Solution 2:

You can collect the strings you want to write to the file in a list (etc.) and then use python's built-in file operations, namely open(<file>) and <file>.write(<string>), as such:

strings = ['hello', 'world', 'today']

# Open the file for (a)ppending, (+) creating it if it didn't exist
f = open('file.txt', 'a+')

for s in strings:
    f.write(s + "\n")

See also: How do you append to a file?

Post a Comment for "How To Append Data To Text File In Python 2.7.11?"