Python outputs wrong value [duplicate] - python

This question already has answers here:
How to get last items of a list in Python?
(5 answers)
Closed 10 months ago.
From the log file I need the last lines. I listed them in [-1:-7]
f_read = open("C:\logs.txt", "r")
status = f_read.readlines()[-1:-7]
print(status)
Python outputs
[]
How can I make it show strings?

[-7:-1]
it must be from lower to upper

Related

Can anyone explain the function of last line in the following code [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 4 months ago.
str_1= input("Enter:")
for i in range(len(str_1)+1):
print(str_1[:i])
It means get all the characters starting from the i'th position for every iteration

Is there a way to enter an if statement for .isdecimal? [duplicate]

This question already has answers here:
How can I check if a string represents an int, without using try/except?
(23 answers)
What's the difference between str.isdigit(), isnumeric() and isdecimal() in Python?
(4 answers)
Closed last year.
I need to check if a number is a whole or a decimal and input it into an if statement
ex.
if string_name.isdecimal() = true:
print('warning')
I'm trying to check for any overflow.

How to measure how many lines are in a user-inputed string in python [duplicate]

This question already has answers here:
Find how many lines in string
(4 answers)
How to read multiple lines of raw input?
(16 answers)
Closed 3 years ago.
I am trying to get the user to input a string, I then want to count the number of lines they have inputted and save it in a variable to use in a for loop. so far I have tried this:
asciiart = input("Please enter your entire ASCII art: ")
nlines = len(asciiart.split('\n'))
print(nlines)

Why is this split method not working correctly? [duplicate]

This question already has answers here:
why is python string split() not splitting
(3 answers)
Closed 6 years ago.
I'm trying to split
<team>
into just team, here is the code I'm using:
s = "<team>"
s.split(">")[1]
s
'<team>'
s.split(">")[1].split("<")[0]
s
'<team>
As you can see, it's still leaving me with
<team>
anyone know why>
str.split() function returns a list, it does not split the string in place.
You'll need to make a new variable:
s = "<team>"
t = s.split(">")[1]
t

Checking if text file is empty Python [duplicate]

This question already has answers here:
How to check whether a file is empty or not
(11 answers)
Closed 6 years ago.
Is there a way to check if a text file is empty in Python WITHOUT using os ?
What I have tried so far
x = open("friends.txt")
friendsfile = x.readlines()
if friendsfile == None
But I don't think it's the correct way.
Not sure why you wouldn't use os, but I suppose if you really wanted you could open the file and get the first character.
with open('friends.txt') as friendsfile:
first = friendsfile.read(1)
if not first:
print('friendsfile is empty')
else:
#do something

Categories

Resources