+++ title = "How to write array to text file in Python" date = 2021-11-23 lastmod = 2021-11-24 draft = false weight = 100 keywords = ["python write array to text file"] description = "In this article, we will see how to create text file from array using different pre-defined functions in Python." tags = ["Python", "Python Array"] author = "A K M Ali Hasan" postlink = inarticle = true +++
If we want to have a list of values with us, we can go with list or if we want to make it constant we can go with tuple; on the other hand in array we can we can have all the values of same datatype. We can have int, float, string together in one list but we can’t have int, float, string together in Array. Hence in array if all the values are Integer array then it calls int array if all the values are floating point then it calls float array, similar for string array.
i) Open file
ii) Process file
iii) Close file
def main():
And now I am going to open up my file by naming the variable name “outputfile” because we need to output the data to file.
Outputfile = open (‘file.txt’, ‘w’)
Here I am going to user write mode. With write mode we can override any data that might exist; if the file doesn’t exist we are going to create that file. We want the inputs each be on their own line. Therefore we concatenate the inputs with a new line character by using “\n”.
Then we can take 3 inputs
name = input("Please enter a name: ")
address = input("Please enter a address: ")
zipcode = input("Please enter a zip: ")
Then specify output file by outputfile.write()
outputfile.write(name + '\n')
outputfile.write(address + '\n') <br /> outputfile.write(zipcode + '\n')
These are going to write information to our file.
Finally we are going to close our file by
outfile.close()
Then we are going to call our function main()
After saving the code in order to compile and run it open cmd in the path of the file and run it as following
After input the information we can notice no output here, because for the output we write a text file named “info.txt”
def main() :
# open the output file for writing. This will delete any existing data
outputfile = open('info.txt', 'w')
# get three inputs
name = input("Please enter name: ")
address = input("Please enter address: ")
zipcode = input("Please enter zipcode: ")
# save the information into the file
outputfile.write(name + '\n')
outputfile.write(address + '\n')
outputfile.write(zipcode + '\n')
# close the file
outputfile.close()
#call the main function
main()
Now if I want to add more persons information we need to use append function "a” instead of write "w”.
Outputfile = open (‘file.txt’, ‘a’)
By using append function we can enter multiple person’s information. If we try write function “w” for multiple person’s information input it will override not append the information.
def main() :
# open the output file for writing and appending. This will append with any existing data
outputfile = open('info.txt', 'a')
# get three inputs
name = input("Please enter name: ")
address = input("Please enter address: ")
zipcode = input("Please enter zipcode: ")
# save the information into the file
outputfile.write(name + '\n')
outputfile.write(address + '\n')
outputfile.write(zipcode + '\n')
# close the file
outputfile.close()
#call the main function
main()