Questions tagged [readlines]

This tag refers to the `readlines` method available on file objects in Python.

The method's schematics are below:

readlines([size:int]) -> list of strings, each a line from the file.

readlines calls the readline method repeatedly and returns a list of the lines so read. The size argument, if supplied, sets an approximate bound on the total number of bytes in the lines returned.

499 questions
2025
votes
28 answers

How to read a file line-by-line into a list?

How do I read every line of a file in Python and store each line as an element in a list? I want to read the file line by line and append each line to the end of the list.
Julie Raswick
  • 20,403
  • 3
  • 16
  • 3
647
votes
14 answers

How to read a file without newlines?

In Python, calling e.g. temp = open(filename,'r').readlines() results in a list in which each element is a line from the file. However, these strings have a newline character at the end, which I don't want. How can I get the data without the…
Yotam
  • 9,789
  • 13
  • 47
  • 68
389
votes
11 answers

Getting rid of \n when using .readlines()

I have a .txt file with values in it. The values are listed like so: Value1 Value2 Value3 Value4 My goal is to put the values in a list. When I do so, the list looks like this: ['Value1\n', 'Value2\n', ...] The \n is not needed. Here is my code: t…
TDNS
  • 4,125
  • 2
  • 17
  • 17
76
votes
10 answers

Break string into list of characters in Python

Essentially I want to suck a line of text from a file, assign the characters to a list, and create a list of all the separate characters in a list -- a list of lists. At the moment, I've tried this: fO = open(filename, 'rU') fL =…
FlexedCookie
  • 781
  • 1
  • 5
  • 4
49
votes
2 answers

Python readlines() usage and efficient practice for reading

I have a problem to parse 1000's of text files(around 3000 lines in each file of ~400KB size ) in a folder. I did read them using readlines, for filename in os.listdir (input_dir) : if filename.endswith(".gz"): f =…
Learner
  • 1,685
  • 6
  • 30
  • 42
44
votes
5 answers

Undo a file readline() operation so file-pointer is back in original state

I'm browsing through a Python file pointer of a text file in read-only mode using file.readline() looking for a special line. Once I find that line I want to pass the file pointer to a method that is expecting the file pointer to be at the START of…
MikeN
  • 45,039
  • 49
  • 151
  • 227
40
votes
3 answers

Does readlines() return a list or an iterator in Python 3?

I've read in "Dive into Python 3" that: "The readlines() method now returns an iterator, so it is just as efficient as xreadlines() was in Python 2". See: Appendix A: Porting Code to Python 3 with 2to3: A.26 xreadlines() I/O method. I'm not sure…
snakile
  • 52,936
  • 62
  • 169
  • 241
26
votes
4 answers

Removing \r\n from a Python list after importing with readlines

I have saved a list of ticker symbols into a text file as follows: MMM ABT ABBV ANF .... Then I use readlines to put the symbols into a Python list: stocks = open(textfile).readlines() However, when I look at the list in it contains Windows…
Justin
  • 261
  • 1
  • 3
  • 3
19
votes
4 answers

Python read text file from second line to fifteenth

I have a text file and I need to read from the seconds line to to 15th line including. I've tried some methods but no method worked for me... I'd be happy if anyone could help me ... thanks a lot!
Den1al
  • 617
  • 1
  • 10
  • 16
18
votes
5 answers

How to get length of a list of lists in python

So, if I have a list called myList I use len(myList) to find the number of elements in that list. Fine. But how do I find the number of lists in a list? text = open("filetest.txt", "r") myLines = text.readlines() numLines=len(myLines) print…
user3022562
  • 181
  • 1
  • 1
  • 3
18
votes
4 answers

How to create fake text file in Python

How can I create a fake file object in Python that contains text? I'm trying to write unit tests for a method that takes in a file object and retrieves the text via readlines() then do some text manipulation. Please note I can't create an actual…
user1454693
  • 347
  • 2
  • 5
  • 13
17
votes
5 answers

Is there a difference between : "file.readlines()", "list(file)" and "file.read().splitlines(True)"?

What is the difference between : with open("file.txt", "r") as f: data = list(f) Or : with open("file.txt", "r") as f: data = f.read().splitlines(True) Or : with open("file.txt", "r") as f: data = f.readlines() They seem to produce…
Bermuda
  • 181
  • 1
  • 1
  • 8
15
votes
4 answers

sys.stdin.readlines() hangs Python script

Everytime I'm executing my Python script, it appears to hang on this line: lines = sys.stdin.readlines() What should I do to fix/avoid this? EDIT Here's what I'm doing with lines: lines = sys.stdin.readlines() updates = [line.split() for line in…
Bo A
  • 3,144
  • 2
  • 33
  • 49
12
votes
2 answers

What substitutes xreadlines() in Python 3?

In Python 2, file objects had an xreadlines() method which returned an iterator that would read the file one line at a time. In Python 3, the xreadlines() method no longer exists, and realines() still returns a list (not an iterator). Does Python 3…
snakile
  • 52,936
  • 62
  • 169
  • 241
11
votes
2 answers

readLines function with new version of R

My function is: create_matrix <- function() { cat("Write the numbers of vertices: ") user_input <- readLines("stdin", n=1) user_input <- as.numeric(user_input) print(user_input) } With the version 3.5.0, after i entered the data the…
Dr.mincode
  • 121
  • 5
1
2 3
33 34