I am writing a program that takes the text written in a text document called datafile.txt, then prints what is in the file. The text that is in the file is is "Hello World", but the text that prints is "['Hello World']" How do i make it so the characters [, ], and ' do not appear.

file = open("datafile.txt", "r")
lines = file.readlines()
SaveDir = lines
print SaveDir

2 Answers 2


The issue is that file.readlines() returns a list of strings - one for each line in the file - rather than a single string. The [' and '] characters are just python's way of indicating the containing list object. It doesn't know ahead of time that your file only contains a single line.

You can print the individual strings (in this case there is only one) by looping over the list e.g.

for SaveDir in lines :
    print SaveDir
  • How this have to do with gtk or glade ? Isn't this clearly an I/O python question and so, off-topic?
    – hytromo
    Jun 12, 2014 at 23:23
  • no luck, the same output occurred @hakermania
    – marc.soda
    Jun 12, 2014 at 23:49
  • Or just use .read() which is preferred over .readlines() anyway.
    – Timo
    Jun 13, 2014 at 9:11

You could try this python code,

import os
file = open("/path/datafile.txt", "r")
lines = file.readlines()
str = ''.join(lines)
print str  # => Hello World

.join() function is used to convert a list into string. It prints the file contents as it is without displaying it in list.

Not the answer you're looking for? Browse other questions tagged .