Search code examples
pythonfilefile-length

How can I check the length of the python code?


Is there a method to check the length of the CURRENT full code?

Similar to this:

#The file is the current code, not an another
file = open("main.py", "r")
length_in_lines = file.linelength()
length_in_characters = file.chars()

If you know a similar method to solve this or fix the errors, on the code thank you :D


Solution

  • Try the following:

    file = open("main.py", "r")
    length_in_lines = len(file.readlines())
    file.seek(0)
    length_in_char = len(file.read())
    

    readlines() reads all the lines of the file in a list.

    read() reads the whole file in a string.

    The len() function returns the length of the argument.