I\'m very new to programming (obviously) and really advanced computer stuff in general. I\'ve only have basic computer knowledge, so I decided I wanted to learn more. Thus I
with open("filename.txt", "w+") as file:
for line in file:
print line
This with
statement automatically opens and closes it for you and you can iterate over the lines of the file with a simple for
loop
This will give you the contents of a file separated, line-by-line in a list:
with open('xyz.txt') as f_obj:
f_obj.readlines()
to input a file:
fin = open(filename) #filename should be a string type: e.g filename = 'file.txt'
to output this file you can do:
for element in fin:
print element
if the elements are a string you'd better add this before print:
element = element.strip()
strip()
remove notations like this: /n