Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In general the open.readlines() pattern suggested in another answer works quite well and is easier to work when you're developing the file processing that you have to do, but it does lead to the entire textual file content being allocated in main memory. If you want to process the lines as they are read from the file, you can use that the file itself already knows how to "iterate" over its lines:

sage: L = [ RDF(l.strip()) for l in open('/path/to/your/file.txt', 'r')]

In general the open.readlines() pattern suggested in another answer works quite well and is easier to work when you're developing the file processing that you have to do, but it does lead to the entire textual file content being allocated in main memory. If you want to process the lines as they are read from the file, you can use that the file itself already knows how to "iterate" over its lines:

sage: L = [ RDF(l.strip()) for l in open('/path/to/your/file.txt', 'r')]

While this works, it's better to use a with clause to ensure the file is closed:

sage: with  open('/path/to/your/file.txt', 'r') as f:
....:     L = [RDF(l.strip()) for l in f]