How can you check the length of a file / list and then use the value in an "if-loop" in python?

I want to create a "if-sats" that must check the amount of elements(numbers) in a textfile, if the amount is less then 5
do this...
else
do that..

Here is a little experiment you can do ...

# assume this would be the content of your data file
data = """\
1
2
3
4
5
6
"""

fname = "mynumbers.dat"

# write your test data file
fout = open(fname, "w")
fout.write(data)
fout.close()

# now read the test data file
fin = open(fname, "r")
data_str = fin.read()
fin.close()

# convert to a list
data_list = data_str.split()

# show the data_list
# note that the list items are strings
# you will have to do further processing 
# to change them to numbers
print( data_list )  # ['1', '2', '3', '4', '5', '6']

# get and show number of items in the data_list
length = len(data_list)
print( length )  # 6

# now you can use length in your if/else statements

Thank you, i will look into it

I have a follow up question, i cant get a grip of this random stuff..:)
How do i change this code line
del data_list[3:5+1]

so instead of removing specifik elements i want to remove random ones, amount of elements to be removed as well.

very thankful for this forum and i start to like python more and more.

I have a follow up question, i cant get a grip of this random stuff..:)
How do i change this code line
del data_list[3:5+1]

so instead of removing specifik elements i want to remove random ones, amount of elements to be removed as well.

very thankful for this forum and i start to like python more and more.

I am not sure that this question has anything to do with your above question. You can use import random and then random.randint(0, 10) to generate a random number in the range (and including) 0 to 10.

Check it out and if you still have questions, perhaps ask it in a new thread.

Thanks , you are right but i didint want to start a new thread for this small question:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.