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.txtfile (Probably it's empty, because you mentioned the"test"is not visible in the console, it means the script doesn't run in theforloop, in other word: the length oflinesiterator is zero).I have written a working code, you can see below the code and the test file with the output of script.
Code:
Content of
data.txt:Test:
EDIT:
The OP shared the complete source code and the used
data.txtwhich containscrlfcharacters (Details about that chars). It means the lines have to be striped withrstripmethod.In this case only the
check_idfunction is related so I share only that modified function: