Reading line by line from a file in python [duplicate] - python

This question already has answers here:
How to read a file line-by-line into a list?
(28 answers)
Closed 5 years ago.
In order to extract some values from this file :
I need to read it line by line.
I tried to read line by line first but i don't know why it doesn't work.
I tried this :
#! /usr/bin/python
file = open('/home/results/err.txt')
for line in file.readline():
print line
EDIT:
Problem: working but not showing this lines (this is the file)
Just the last line of them which is: (this is what is generated)

You need to iterate through the file rather than the line:
#! /usr/bin/python
file = open('/home/results/err.txt')
for line in file:
print line
file.readline() only reads the first line. When you iterate over it, you are iterating over the characters in the line.

file.readline() already reads one line. Iterating over that line gives you the individual characters.
Instead, use:
for line in file:
…

Try this :
#! /usr/bin/python
file = open('/home/results/err.txt')
for line in file.readlines():
print line

You might want to use a context manager with that to automatically close your opened file after the lines have been read, that is to ensure nothing unexpected happens to your file while python is processing it.
with open('/home/results/err.txt', 'r') as file:
for line in file:
print line
readline() would read your file line-by-line but iterating over that would print the letters individually.

Related

Is there a way to get lines of a txt file as an input? [duplicate]

This question already has answers here:
Is it possible to modify lines in a file in-place?
(5 answers)
Closed 2 months ago.
Let's suppose I have a file, demo.txt. This file contains three lines which are:
- line 1
- line 2
- line 3
I want to edit this file lines while iterating the demo.txt. For example, the program started to iterating, and now we are at line 1.
Now we modify this line to line 1.1 and press Enter the continue to iterate to the line 2. When I pressed enter the line 1.1 is saved to demo.txt file.
Then the same thing for line 2, i have changed line 2 to line 2.2 and pressed enter. The same thing for line 3: Change to line 3.3, press Enter to save and finished. This should be apply to every line. Is there a way to achieve that?
Like others explained in comments, you should not read a file while changing its content at the same time. You'll get either bad performance or inconsistent results (or both). You should either buffer the input for reading or buffer the output for writing.
Here is a solution that buffers the input file:
from pathlib import Path
def main():
path = Path("demo.txt")
lines = path.read_text().splitlines()
with path.open("w") as f:
for line in lines:
new_line = transform(line)
f.write(f"{line}\n")
if __name__ == "__main__":
main()
Here, transform is a function that transform a line into a new line. the input file demo.txt is overwritten.
The other way around, i.e. buffering writes, would be:
from pathlib import Path
def main():
path = Path("demo.txt")
new_lines = []
with path.open() as f:
for line in f:
new_line = transform(line)
lines.append(f"{new_line}")
path.write_text("\n".join(new_lines))
if __name__ == "__main__":
main()
Alternatively, you can create a new file with a different name as suggested in another answer.
As I mentioned in comments, we should not save in the same file once the line is processed which you are currently accessing/iterating over. Simply take the two files and open them one at a time and modify accordingly.
import os
with open('demo.txt') as file, open ('new.txt', 'w+') as file2:
for line in file:
s = input()
if len(s) ==0:
line = "modified\n"
file2.write(line)
print(line)
file.close()
file2.close()
os.replace('new.txt', 'demo.txt')
Your new demo.txt file looks like this
modified
modified
modified
What's happening here:
Open your file and a temporary dummy file at once.
Wait for user to hit enter. once he hits enter python reads line1 and saves to new line.
once all the lines are finished, close the both files.
Rename your temporary file with old original file name, i.e,, demo.txt

How can i avoid new line at the end of the file?

i need to use
for line in doc.split('\n'):
and do some operation on each line but i got at the end of the file
empty line as i think it split a new line every time ! how can i avoid this problem ?
Please rephrase your question, since it is not very clear.
Anyway, if you are working with a text file you can just use:
with open("path_to_soruce_textfile", "r") as src, open("path_to_dest_textfile", "w") as dst:
for line in src.readlines(): # this gives you a list of lines with each line finishing with \n
processed_line = modidy(line)
dst.write(processed_line) # make sure \n is at the end of each line when you write
# the file is automatically closed

Eliminate blank lines in file read Python [duplicate]

This question already has answers here:
Python program prints an extra empty line when reading a text file
(4 answers)
Closed 3 years ago.
I'm trying to read a file into a list using Python. But when I do that the list appears with blank lines after each entry. The source file doesn't have that!
My code:
aws_env_list="../../../source_files/aws_environments/aws_environments_all.txt"
with open(aws_env_list, 'r') as aws_envs:
for line in aws_envs:
print(line)
Each line prints out with a blank line after each entry:
company-lab
company-bill
company-stage
company-dlab
company-nonprod
company-prod
company-eng-cis
The source file looks like this:
company-lab
company-bill
company-stage
company-dlab
company-nonprod
company-prod
company-eng-cis
How do I get rid of the blank line after each entry?
When you iterate over a file line-by-line using:
for line in aws_envs:
The value of line includes the end-of-line character...and the print command, by default, adds an end-of-line character to your output. You can suppress that by setting the end parameter to an empty value. Compare:
>>> print('one');print('two')
one
two
Vs:
>>> print('one', end='');print('two')
onetwo
Your file has a new line character at the end of each line like:
company-lab\n
company-bill\n
company-stage\n
company-dlab\n
company-nonprod\n
company-prod\n
company-eng-cis # not here though this has an EOF (end-of-file) character.
So your call to print(line) is including these in the print! You can avoid this like:
aws_env_list="../../../source_files/aws_environments/aws_environments_all.txt"
with open(aws_env_list, 'r') as aws_envs:
for line in aws_envs.readlines():
print(line.strip()) # just strip the \n away!
UPDATE
If you would like to compute with just the text and not the newline character you can strip it away like this:
aws_env_list="../../../source_files/aws_environments/aws_environments_all.txt"
with open(aws_env_list, 'r') as aws_envs:
for line in aws_envs.readlines():
line = line.strip() # You can strip it here and reassign it to the same variable
# Now all your previous code with the variable 'line' will work as expected
print(line) # no need to strip again
do_computations(line) # you can pass it to functions without worry

File iterator in Python does not return last line

I'm new to Python and I have simple script to read file and print it to output:
f = open('somefile.txt', mode='rt', encoding='utf-8')
for line in f:
sys.stdout.write(line)
f.close()
If file ends with new line symbol it prints all lines from file. But if last line does not contain new line symbol it ends with one before last. I run script in Windows environment and use python 3.6.
For example file for first case:
Some text here
Some here
and here
And script's output is:
Some text here
Some here
But when last line contains new line symbol output is:
Some text here
Some here
and here
What I'm doing wrong?
To ensure that the buffer is empty, you could add sys.stdout.flush() after your loop completes to make sure it's empty (as sys.stdout is line buffered).

Deleting the first line of a text file in python [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Editing specific line in text file in python
I am writing a software that allows users to write data into a text file. However, I am not sure how to delete the first line of the text file and rewrite the line. I want the user to be able to update the text file's first line by clicking on a button and inputing in something but that requires deleting and writing a new line as the first line which I am not sure how to implement. Any help would be appreciated.
Edit:
So I sought out the first line of the file and tried to write another line but that doesn't delete the previous line.
file.seek(0)
file.write("This is the new first line \n")
You did not describe how you opened the file to begin with. If you used file = open(somename, "a") that file will not be truncated but new data is written at the end (even after a seek on most if not all modern systems). You would have to open the file with "r+")
But your example assumes that the line you write is exactly the same length as what the user typed. There is no line organisation in the files, just bytes, some of which indicate line ending.
Wat you need to do is use a temporary file or a temporary buffer in memory for all the lines and then write the lines out with the first replaced.
If things fit in memory (which I assume since few users are going to type so much it does not fit), you should be able to do:
lines = open(somename, 'r').readlines()
lines[0] = "This is the new first line \n"
file = open(somename, 'w')
for line in lines:
file.write(line)
file.close()
You could use readlines to get an array of lines and then use del on the first index of the array. This might help. http://www.daniweb.com/software-development/python/threads/68765/how-to-remove-a-number-of-lines-from-a-text-file-

Categories

Resources