for loop in file handling (python) [duplicate] - python

This question already has answers here:
Understanding file iteration in Python
(5 answers)
Closed 2 years ago.
f = open("food.txt", "r")
for line in f:
print(line)
I don't understand how the above for loop reads the file line by line? Why not character by character or word by word?
please explain.

That's how Python works, open creates a file-object.
If you look at I/O documentation for file objects by default they read line by line!
In words from documentation: "this is memory efficient, fast, and leads to simple code"
In the previous versions of Python(2.2-), you had to specify byte limit for the same functionality!
For characters you can do:
for line in f:
for c in line: print(c)
For words you can do:
for line in f:
for w in line.split(): print(w)

Related

what is the fgets() equivalent in Python [duplicate]

This question already has answers here:
Read only the first line of a file?
(8 answers)
Closed 3 months ago.
I am currently working with file handling in Python.
I have problem in copying the string value of the file.
I wanted to copy the string from file and store it to a variable, just like in C
example: this is how we do in C
FILE *fptr = fopen("read.txt", "r");
fgets(charVar, 100, fptr);
where we store the string file to charVar.
So is there a fgets() function equivalent to python?
You can pass the limit argument to readline for a file object which would have similar behavior of stopping on a max character or a newline. Example text file:
01234567890123456789
01234567890123456789
with open("test.txt", "r") as f:
while data := f.readline(8):
print("line:", data)
Outputs:
line: 01234567
line: 89012345
line: 6789
line: 01234567
line: 89012345
line: 6789

Python : Why I need to reopen a file to succeed in these two manipulations [duplicate]

This question already has answers here:
Why can't I call read() twice on an open file?
(7 answers)
Closed 2 years ago.
This should be basic but I can't find a quick response to my question, sorry if this is a double.
I am writing a small code to learn how to manipulate files and count the number of lines, words and of character inside a txt file.
Could you please explain why in the code below if I don't reload for the second time the file using another with open(), the code fails to count the len(f.read) properly? Without it, it returns 0.
Comments to improve the code are welcomed.
def wc(nomFichier):
nb_car=0
nb_words=0
nb_lig=0
with open(nomFichier) as f:
for line in f:
words = line.split()
nb_words += len(words)
nb_lig +=1
with open(nomFichier) as f: #Why I need to reload the file?
nb_car=len(f.read()) #f.read().stripignore to ignore ligne\n
f.close()
return (nb_car, nb_words, nb_lig)
You don't need to reopen the file.
def word_count(file_path):
count_chars=0
count_words=0
count_lines=0
with open(file_path) as f:
for line in f:
count_chars += len(line)
count_words += len(line.split())
count_lines +=1
return (count_chars, count_words, count_lines)
Note that I changed the variable names to one I think are more appropriate.

List strings have "\n" at the end of them, when individually printed "\n" is gone [duplicate]

This question already has answers here:
readlines gives me additional linebreaks python2.6.5
(3 answers)
Closed 8 years ago.
I'm reading strings from lines in a text file and then putting them into a list, simple enough. However when I return the List, the strings have a new line character at the end of them. When i try printing a value individually with print list[0] for example the new line character will not be present. How do I make it so the list values don't have newline characters in them to begin with? Why is this happening anyway?
Edit: I didn't think code would matter but even in the following i will have problems:
file = open("test.txt", "U")
test = []
for line in file:
line.rstrip()
test.append(line)
print test
Update: I looked up stripping new line characters and using .rstrip() doesn't work
you can try
file = open("test.txt", "U")
test = []
for line in file:
line = line.replace("\r\n","").replace("\n","")
test.append(line)
print test

How do I read each line of a file and add if statements? [duplicate]

This question already has answers here:
How to read a file without newlines?
(12 answers)
How can I read inputs as numbers?
(10 answers)
Closed 7 months ago.
I'm pretty new to Python, but I'm trying to learn. One idea I had for a simple script was to have a script that reads and writes to a log file during execution. Then based on what's in that log file, helps dictate what the script does the next time it's ran.
Reading and writing to a file seems simple enough in Python,
f = open('textfile.txt', 'r+')
Reading and print out each line of a file seems simple too,
for line in f:
print line
line=f.next()
print line
But, how do I incorporate an IF statement when reading a file to do something based on what was read?
for line in f
if line == '1':
print "Works!"
else:
print line
line=f.next()
print line
f.close()
Output
1
2
3
4
5
The problem is that line is equal to
'1\n'
You first need to remove the newline character by doing first in your loop:
line = line.rstrip()
Another possibility could be, in this particular case in which you expect integers, to cast the line string into an integer, and then to compare to an integer:
line = int(line)
if line == 1:
# and so on

Reading lines beyond SUB in Python [duplicate]

This question already has answers here:
Line reading chokes on 0x1A
(2 answers)
Closed 4 years ago.
Newbie question. In Python 2.7.2., I have a problem reading text files which accidentally seem to contain some control characters. Specifically, the loop
for line in f
will cease without any warning or error as soon as it comes across a line containing the SUB character (ascii hex code 1a). When using f.readlines() the result is the same. Essentially, as far as Python is concerned, the file is finished as soon as the first SUB character is encountered, and the last value assigned line is the line up to that character.
Is there a way to read beyond such a character and/or to issue a warning when encountering one?
On Windows systems 0x1a is the End-of-File character. You'll need to open the file in binary mode in order to get past it:
f = open(filename, 'rb')
The downside is you will lose the line-oriented nature and have to split the lines yourself:
lines = f.read().split('\r\n') # assuming Windows line endings
Try opening the file in binary mode:
f = open(filename, 'rb')

Categories

Resources