Python readlines() 3.X to 2.X - python

I have a script originally written for python 3.5+. I am needing to convert it to 2.6.2.
In the script I utilize readlines() which I believe behaves differently between the two versions.
Specifically I am using the readlines() to retrieve data from a txt file that is separated by line breaks.
Here is a snippit:
t=open(supportID+"stmp.txt","r")
timeIn=(t.readlines())
a=(str(timeIn))
supportID=(t.readlines())
b=(str(supportID))
branch=(t.readlines())
c=(str(branch))
clientID=(t.readlines())
d=(str(clientID))
problem=(t.readlines())
e=(str(problem))
solution=(t.readlines())
f=(str(solution))
timeOut=(t.readlines())
g=(str(timeOut))
In my script for 3.x I had a '1' in each of the readlines() and it performed as needed, however with 2.x this does not work. I have tried entering values 1-7 and blank as seen above.
With some research I discovered that some 2.x users use with open(filename)
Is this the preferred method or is there a way to alter my original to make it work?
EDIT:
So im going to use this format
with open(supportID+"stmp.txt") as t:
for line in t:
print(line)
I plugged this in and it works, printing each line as a line in my shell. Now I will want to use this to assign each line to a variable.
EDIT 2:
This is currently working for my environment but is not best practice for this function. Reads in each line and assigns each line to a variable.
t=open(supportID+"stmp.txt","r")
lines = t.readlines()
t.close()
a=lines[0]
b=lines[1]
c=lines[2]
...

To read in each line from the file and assign to a variable I used this method for python 2.6.2
t=open(supportID+"stmp.txt","r")
lines = t.readlines()
t.close()
a=lines[0]
b=lines[1]
c=lines[2]
...

try this code: (I assume it will work with python version >= 2.6)
with open('lines.txt','r') as f:
lines = f.readlines()
for line in lines:
print(line.strip())

Related

Reading a simple .txt file with python, why does this simple code output every line? How does python know it should do that?

New to programming, newer to python ;) trying to learn file operations.
I dont understand how the code below works. what happens when open() is called with a for loop and why is this different to another python function .read(number_of_elements) ?
i have a test.txt file which is just numbered from 1) to 10), each on a new line. So a txt file with 10 lines. Why does python show the whole txt file using this for loop? Why does it know to print every line, at each new line? is it because Python converts the .txt into a list and then loops through it?
i have tried doing
print(f)
but this returns some information about the test.txt file, and did not print the content of the file like i expected.
python code:
with open("test.txt", "r") as f:
for line in f:
print(line)
test.txt file:
1)
2)
3)
4)
...
is it because Python converts the .txt into a list and then loops through it
Effectively, yes. The code can be rewritten as:
file = open("test.txt", "r")
for line in file:
print(line)
file.close()
There is a bit of "magic" involved, but basically the File object in file knows that if it's being iterated on (via for line in file), it should give one line per each iteration.
Iterating a file per-line is often what you want to do - so python offers a quick and easy way to do just that. If you want the whole file in a single variable, there's also a method for that.
The variable f is your whole document.
Let's talk about with the for line in f::
By using this line you are looping through each line of the text file. so each time in the loop the value of line is your new line of file and when the print(line) runs it will print each line for you with a \n which means goes to next line.
You can reach more examples and information using this link: File
in Python

How can I incorporate a text file into the body of my Python script?

Currently, my code is reading an external text file, using:
text_file = open("file.txt", 'r', 0)
my_list = []
for line in text_file
my_list.append(line.strip().lower())
return my_list
I would like to send my code to a friend without having to send a separate text file. So I am looking for a way of incorporating the content of the text file into my code.
How can I achieve this?
If I convert the text file into list format ([a, b, c, ...]) inside MS notepad using replace function, and then try to copy & paste list into Python IDE (I'm using IDLE), the process is hellishly memory intensive: IDLE tries to string out everything to the right in one line (i.e. no word wrap), and it never ends.
I'm not totally sure what you're asking, but if I'm guessing what you mean correctly, you could do this:
my_list = ['line1', 'line2']
Where each is a line from your text file.
Just put all the file contents into ONE MASSIVE string:
with open('path/to/my/txt/file') as f:
file_contents = f.read()
So now, your friend can do:
for line in file_contents.split('\n'):
#code
which is equivalent to
with open('path/to/file') as f:
for line in f:
#code
Hope this helps
I would suggest
assign the contents of the file to a variable in another py file
read the value by importing it in you program
that way the py file will be converted to pyc (send that), or py2exe will take care of it..
and would not allow your friend to mess with the contents...
You could also do something like:
my_file_contents = """file_contents_including_newlines"""
for line in my_file_contents.split('\n'): # Assuming UNIX line ending, else split '\r\n'
*do something with "line" variable*
Note the use of triple quotes around the text to be sent. This would work for non-binary data.

readline and readlines methods missing from python 3.2?

Did they remove file.readline() and file.readlines() from python 3.2? If yes what did they replace it with?
While there is no file type any more in Python 3.x, the various types in the io module that replace the old file type still support f.readline() and f.readlines(). You actually don't need those methods, though, since they can be substituted by next(f) and list(f).
Here is the documentation (well, tutorial) for Python 3.2.
readline and readlines are still a part of Python.
No they did not.
f = open("file", "r")
f.readlines()
is working for me, Python 3.2.
EDIT: it produces an io object (not file).
I had problems, too. However, when I included an
import readline
at the top of my script, everything worked fine. It appears that it has to be imported explicitly now.

Python's equivalent to Ruby's File.read method

I'm trying to read the contents of a file in a single method call.
I don't want to have to worry about opening the file, reading from the file, and then closing the file (3 method calls).
I just want the content.
In ruby, there is File.read("/path/to/file"), which returns the contents of that file and properly closes it. Is there an equivalent in Python?
You can concatenate two instructions to get the same behaviour :/. But then the file isn't properly closed.
file = open("/path/to/file","r").read()
edit:
Best option as far as I know leaves you needing 2/3 you mention. Just use the with statement so you don't have to worry about closing said file.
with open("/path/to/file","r") as file:
text = file.read()
You can use a Context Manager in Python, which is available from Python 2.5.
with open('yourfile') as f:
contents = f.read()
It will automatically, open and close the file for you. The default mode is 'r' which stands for reading.
There is no such function included with Python. It's simple enough to define one, though.
def read_whole_file(path):
with open(path) as f:
return f.read()

How do you read a file into a list in Python? [duplicate]

This question already has answers here:
How to read a file line-by-line into a list?
(28 answers)
Closed 8 years ago.
I want to prompt a user for a number of random numbers to be generated and saved to a file. He gave us that part. The part we have to do is to open that file, convert the numbers into a list, then find the mean, standard deviation, etc. without using the easy built-in Python tools.
I've tried using open but it gives me invalid syntax (the file name I chose was "numbers" and it saved into "My Documents" automatically, so I tried open(numbers, 'r') and open(C:\name\MyDocuments\numbers, 'r') and neither one worked).
with open('C:/path/numbers.txt') as f:
lines = f.read().splitlines()
this will give you a list of values (strings) you had in your file, with newlines stripped.
also, watch your backslashes in windows path names, as those are also escape chars in strings. You can use forward slashes or double backslashes instead.
Two ways to read file into list in python (note these are not either or) -
use of with - supported from python 2.5 and above
use of list comprehensions
1. use of with
This is the pythonic way of opening and reading 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.
the .strip() is used for each line of the file to remove \n newline character that each line might have. When the with ends, the file will be closed automatically for you. This is true even if an exception is raised inside of it.
2. use of list comprehension
This could be considered inefficient as the file descriptor might not be closed immediately. Could be a potential issue when this is called inside a function opening thousands of files.
data = [line.strip() for line in open("C:/name/MyDocuments/numbers", 'r')]
Note that file closing is implementation dependent. Normally unused variables are garbage collected by python interpreter. In cPython (the regular interpreter version from python.org), it will happen immediately, since its garbage collector works by reference counting. In another interpreter, like Jython or Iron Python, there may be a delay.
f = open("file.txt")
lines = f.readlines()
Look over here. readlines() returns a list containing one line per element. Note that these lines contain the \n (newline-character) at the end of the line. You can strip off this newline-character by using the strip()-method. I.e. call lines[index].strip() in order to get the string without the newline character.
As joaquin noted, do not forget to f.close() the file.
Converting strint to integers is easy: int("12").
The pythonic way to read a file and put every 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 lines contains a number,
numbers =[int(e.strip()) for e in lines]
You need to pass a filename string to open. There's an extra complication when the string has \ in it, because that's a special string escape character to Python. You can fix this by doubling up each as \\ or by putting a r in front of the string as follows: r'C:\name\MyDocuments\numbers'.
Edit: The edits to the question make it completely different from the original, and since none of them was from the original poster I'm not sure they're warrented. However it does point out one obvious thing that might have been overlooked, and that's how to add "My Documents" to a filename.
In an English version of Windows XP, My Documents is actually C:\Documents and Settings\name\My Documents. This means the open call should look like:
open(r"C:\Documents and Settings\name\My Documents\numbers", 'r')
I presume you're using XP because you call it My Documents - it changed in Vista and Windows 7. I don't know if there's an easy way to look this up automatically in Python.
hdl = open("C:/name/MyDocuments/numbers", 'r')
milist = hdl.readlines()
hdl.close()
To summarize a bit from what people have been saying:
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 wish have something in place similar to a 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
If you have multiple numbers per line and you have multiple lines, you can read them in like this:
#!/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()]

Categories

Resources