How do you read a file in a list in Python?

I want the user to call a few random numbers that need to be generated and stored in a file. He gave us this part. The part we have to do is open this file, convert the numbers to a list, then find the mean, standard deviation, etc. Without using simple python built-in tools.

I tried using open , but it gives me invalid syntax (the file name I selected was "number" and it was automatically saved in "My Documents" , so I tried open(numbers, 'r') and open(C:\name\MyDocuments\numbers, 'r') , and none of them worked).

+81
python file
Oct 13 2018-10-10
source share
8 answers
 with open('C:/path/numbers.txt') as f: lines = f.read().splitlines() 

this will give you a list of values โ€‹โ€‹(lines) that you had in your file, with a line feed.

also observe backslashes in Windows path names, as they are also escape characters in strings. Instead, you can use a slash or a double backslash.

+154
Oct 13 2018-10-10
source share

Two ways to read a file to a list in python (note that they are not either) -

  • using with - supported with python 2.5 and higher
  • using list concepts

1. use with

This is a pythonic way to open and read files.

 #Sample 1 - elucidating each step but not memory efficient lines = [] with open("C:\name\MyDocuments\numbers") as file: for line in file: line = line.strip() #or some other preprocessing lines.append(line) #storing everything in memory! #Sample 2 - a more pythonic and idiomatic way but still not memory efficient with open("C:\name\MyDocuments\numbers") as file: lines = [line.strip() for line in file] #Sample 3 - a more pythonic way with efficient memory usage. Proper usage of with and file iterators. with open("C:\name\MyDocuments\numbers") as file: for line in file: line = line.strip() #preprocess line doSomethingWithThisLine(line) #take action on line instead of storing in a list. more memory efficient at the cost of execution speed. 

.strip() used for each line of the file to remove the \n newline character that each line can have. When the with end ends, the file will be automatically closed for you. This is true even if an exception occurs in it.

2. using list comprehension

This can be considered inefficient because the file descriptor cannot be closed immediately. It can be a potential problem when it is called inside a function that opens thousands of files.

 data = [line.strip() for line in open("C:/name/MyDocuments/numbers", 'r')] 

Note that closing the file is implementation dependent. Usually unused variables are garbage collected by the python interpreter. In cPython (a version of the regular interpreter from python.org) this will happen immediately, since the garbage collector works by reference counting. There may be a delay in another interpreter, such as Jython or Iron Python.

+75
Oct 13 2018-10-10
source share
 f = open("file.txt") lines = f.readlines() 

Take a look here . readlines() returns a list containing one line per element. Note that these lines contain the \n character (newline character) at the end of the line. You can remove this newline with the strip() method. That is, call lines[index].strip() to get a line without a newline character.

As joaquin noted, don't forget the f.close() file.

Converting strint to integers is easy: int("12") .

+46
Oct 13 2018-10-10
source share

The pythonic way to read a file and put all the lines in a list:

 from __future__ import with_statement #for python 2.5 with open('C:/path/numbers.txt', 'r') as f: lines = f.readlines() 

Then, assuming that each line contains a number,

 numbers =[int(e.strip()) for e in lines] 
+14
Oct 13 2018-10-10
source share

You need to pass the file name string to open . There is an additional complication when a string has \ in it, because it is a special string escape character for Python. You can fix this by doubling each as \\ or by putting r in front of the line as follows: r'C:\name\MyDocuments\numbers' .

Edit: Editing a question makes it completely different from the original, and since none of them were from the original poster, I'm not sure if they were attacked. However, he points to one obvious thing that may have been overlooked, and how to add โ€œMy Documentsโ€ to the file name.

In the English version of Windows XP, My Documents is actually C:\Documents and Settings\name\My Documents . This means that the open call should look like this:

 open(r"C:\Documents and Settings\name\My Documents\numbers", 'r') 

I assume you are using XP because you call it My Documents - it has changed in Vista and Windows 7. I donโ€™t know if there is an easy way to automatically find this in Python.

+8
Oct 13 2018-10-10
source share
 hdl = open("C:/name/MyDocuments/numbers", 'r') milist = hdl.readlines() hdl.close() 
+5
Oct 13 2018-10-10
source share

To summarize a bit of what people said:

 f=open('data.txt', 'w') # will make a new file or erase a file of that name if it is present f=open('data.txt', 'r') # will open a file as read-only f=open('data.txt', 'a') # will open a file for appending (appended data goes to the end of the file) 

If you want something in place like try / catch

 with open('data.txt') as f: for line in f: print line 

I think @movieyoda code is probably what you should use, however

+4
Oct 13 2018-10-10
source share

If you have several numbers in a line and you have several lines, you can read them as follows:

  #!/usr/bin/env python from os.path import dirname with open(dirname(__file__) + '/data/path/filename.txt') as input_data: input_list= [map(int,num.split()) for num in input_data.readlines()] 
0
Apr 27 '14 at 19:15
source share



All Articles