0
Python working with files reading through
DoES NOT WORK HERE IS THE DESCRIPTION You need to make a program to read the given number of characters of a file. Take a number N as input and output the first N characters of the books.txt file.
5 ответов
+ 2
Kim Hammar-Milliner
That is because the last line of the file has no trailing new line or "\n".
You can use line.strip() to remove the "\n" then use len function (without -1).
Or another method is to use if-else condition, if "\n" is at the end of the line, get the lenght subtracted by 1, otherwise just get the length.
0
my code
file = open("/usercode/files/books.txt", "r")
for line in file:
nums= list(line)
x=len(nums)-1
print(line[0]+str(x))
file.close()
all the test cases fails
0
Noura moussa
0
How are you today to see you tomorrow morning how are
0
file = open("/usercode/files/books.txt")
#your code goes here
n = int(input())
print(file.read(n))
file.close()