1

Is it possible to create a variable in which I can store a list of products instead of using "lines" variable. when creating a text file

#creating a textfile 

text_file= open("productlist.txt", "w")

lines = ("Bed","\n","Couch","\n","Mirror","\n","Television","\n"
         "Tables","\n","Radio")


text_file.writelines(lines)
text_file.close()

text_file = open("productlist.txt")
print(text_file.read())
text_file.close()
4
  • you mean lines = text_file.readlines() maybe ? Commented Sep 4, 2017 at 13:01
  • @Jean-FrançoisFabre No, I want to know if I can be able to use another variable when creating this list lines = ("Bed","\n","Couch","\n","Mirror","\n","Television","\n""Tables","\n","Radio") Commented Sep 4, 2017 at 13:05
  • 1
    I really don't know what you mean. You can create as many variables as you want. Commented Sep 4, 2017 at 13:06
  • Thanks for the clarification, I will try and see if it comes alright Commented Sep 4, 2017 at 13:13

2 Answers 2

1

I believe what you're trying to accomplish is to not write a line break "\n" in there every time, right? Just throw your code into a loop:

#Create text file
text_file = open("productlist.txt", "w")

#Enter list of products
products = ["Bed", "Couch", "Mirror", "Television", "Tables", "Radio"] #Formerly "lines" variable

#Enter each product on a new line
for product in products:
    text_file.writelines(product)
    text_file.writelines('\n')

#Close text file for writing
text_file.close()

#Open text file for reading
text_file = open("productlist.txt")
print(text_file.read())

#Close text file
text_file.close()

If you ever decide you want to append to your document rather than overwrite it every time, just change text_file= open("productlist.txt", "w") to text_file= open("productlist.txt", "a")

If a text document isn't the best format for your list, you might consider exporting to a csv file (which you can open in an excel spreadsheet)

Sign up to request clarification or add additional context in comments.

4 Comments

this makes so much sense and its much more readable, is there a difference when using ( and [ when assigning values to a variable.
Yes, @mkrieger1 explains below in his answer that using (products) will result in a tuple whereas [products] will result in a list. I would do a google search and learn the difference
Why not use with open() btw ?
I have the below code and Im trying to remove a line from the above productlist.txt file but the problem is I still see everything and nothing has been removed import sys f=open("productlist.txt", "r") lines= f.readlines() for line in lines: sys.stdout.write(line) f.close() del lines[2] f=open("productlist.txt", "r") lines= f.read() print(lines) f.close()
0

Instead of writing each line, and the line breaks, individually using writelines, you can create a single string containing all the lines and the line breaks, and write that.

In order to create the combined string from a list of items, you can use join.

products = ["Bed", "Couch", "Mirror", "Television", "Tables", "Radio"]
text_file.write("\n".join(products))

(Note that (a, b, c) creates a tuple, while [a, b, c] creates a list. You may want to learn about the differences, but in this case it doesn't matter.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.