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.

431 questions
2026
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,413
  • 3
  • 13
  • 3
467
votes
9 answers

How to read a file without newlines?

In Python, calling temp = open(filename,'r').readlines() results in a list in which each element is a line in the file. It's a little stupid but still: readlines() also writes newline character to each element, something I do not wish to happen.…
Yotam
  • 7,564
  • 13
  • 42
  • 66
65
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
  • 671
  • 1
  • 5
  • 4
46
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,575
  • 5
  • 23
  • 40
37
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
  • 47,358
  • 57
  • 160
  • 231
37
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
  • 39,649
  • 47
  • 142
  • 222
20
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
  • 201
  • 1
  • 2
  • 3
16
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
  • 161
  • 1
  • 1
  • 3
15
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
  • 469
  • 1
  • 6
  • 15
15
votes
3 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
  • 307
  • 1
  • 5
  • 13
11
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
  • 121
  • 1
  • 8
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
11
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
  • 47,358
  • 57
  • 160
  • 231
11
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,087
  • 1
  • 29
  • 48
9
votes
3 answers

Dealing with readLines() function in R

I'm experiencing a very hard time with R lately. I'm not an expert user but I'm trying to use R to read a plain text (.txt) file and capture each line of it. After that, I want to deal with those lines and make some breaks and changes in the…
user3521631
  • 93
  • 1
  • 1
  • 4
1
2 3
28 29