It suppose to count amount of "-------------------------" lines but it doesn't work, also line with print("test") doesn't shows in console and it always returns 0. But for example line print("hi") works. Program just cannot see my loop and I have no idea why. :(
def check_id():
with open('data.txt', 'r') as f:
lines = f.readlines()
ad = 0
print("hi") # This line works
for i in lines:
print("test") # This line doesn't work
if i == "-------------------------":
ad += 1
return str(ad)
if I need to send full code to solve the problem just ask
I changed mode 'a+' to 'r' so that it can read lines correctly, and it does, but I still can't check array to get amount of this lines. If you have any guesses or solutions write it please.
Edited: Here is my whole code of data.py and text in file data.txt
from datetime import date
date = date.today()
def write_note(line):
with open('data.txt', 'a') as f:
if line == "!quit":
f.write('\n')
f.write("-------------------------")
f.write('\n')
ad = check_id()
f.write(ad)
f.write('\n')
f.write("________________________")
f.write('\n')
else:
f.write(line)
f.write("\n")
def read_note(id):
with open('data.txt', 'r') as f:
pass
def see_all():
with open('data.txt', 'r') as f:
get_lines = f.readlines()
for i in get_lines:
print(i)
return get_lines
def del_note(ad):
with open('data.txt', 'a') as f:
pass
def logs():
pass
def check_id():
with open('data.txt', 'r') as f:
ad = 0
for i in f:
if i == "-------------------------":
ad += 1
return str(ad)
and now txt file:
fugy
hello
hai
bebra
-------------------------
0
________________________
uha
imna
fsjfoe
geso;rsevdn
-------------------------
0 # This one
________________________
I'm trying to make notebook so that it's possible to write notes and read them. Delete func i'll do after. Idea is to make this zeros bigger each time I add note.
I think the problem is with your data.txt
file (Probably it's empty, because you mentioned the "test"
is not visible in the console, it means the script doesn't run in the for
loop, in other word: the length of lines
iterator is zero).
I have written a working code, you can see below the code and the test file with the output of script.
Code:
def check_id():
with open('data.txt', 'r') as opened_file:
ad = 0
print("hi") # This line works
for i in opened_file:
print("test") # This line doesn't work
if i == "-------------------------":
ad += 1
return str(ad)
result = check_id()
print(f"Result: {result}")
Content of data.txt
:
test_1
-------------------------
test_2
-------------------------
test_3
-------------------------
test_4
Test:
> python3 test.py
hi
test
test
test
test
test
test
test
Result: 0
EDIT:
The OP shared the complete source code and the used data.txt
which contains cr
lf
characters (Details about that chars). It means the lines have to be striped with rstrip
method.
In this case only the check_id
function is related so I share only that modified function:
def check_id():
with open('data.txt', 'r') as f:
ad = 0
for i in f:
# The cr and lf characters should be removed from line. Please see the above reference for details.
if i.rstrip() == "-------------------------":
ad += 1
return str(ad)
result = check_id()
print(result). # Result is 4