如何检查 python 代码的长度?
How can I check the length of the python code?
有没有办法检查当前完整代码的长度?
与此类似:
#The file is the current code, not an another
file = open("main.py", "r")
length_in_lines = file.linelength()
length_in_characters = file.chars()
如果你知道类似的方法来解决这个问题或修复错误,在代码上谢谢你:D
尝试以下操作:
file = open("main.py", "r")
length_in_lines = len(file.readlines())
file.seek(0)
length_in_char = len(file.read())
readlines()
读取列表中文件的所有行。
read()
以字符串形式读取整个文件。
len()
函数returns参数的长度。
你可以试试这个:
with open('script.py') as file:
lines = len(file.readlines())
file.seek(0)
chars = len(file.read())
print('Number of lines: {}'.format(lines))
print('Number of chars: {}'.format(chars))
您首先会得到一个包含所有行的列表,并获取列表的长度(即行数),然后您会将整个文件作为字符串读取并计算其中的所有字符。
使用下面的代码读取字符和代码行。
file = open("main.py", "r")
print(len(file.readlines()))
print(len(file.read()))
有没有办法检查当前完整代码的长度?
与此类似:
#The file is the current code, not an another
file = open("main.py", "r")
length_in_lines = file.linelength()
length_in_characters = file.chars()
如果你知道类似的方法来解决这个问题或修复错误,在代码上谢谢你:D
尝试以下操作:
file = open("main.py", "r")
length_in_lines = len(file.readlines())
file.seek(0)
length_in_char = len(file.read())
readlines()
读取列表中文件的所有行。
read()
以字符串形式读取整个文件。
len()
函数returns参数的长度。
你可以试试这个:
with open('script.py') as file:
lines = len(file.readlines())
file.seek(0)
chars = len(file.read())
print('Number of lines: {}'.format(lines))
print('Number of chars: {}'.format(chars))
您首先会得到一个包含所有行的列表,并获取列表的长度(即行数),然后您会将整个文件作为字符串读取并计算其中的所有字符。
使用下面的代码读取字符和代码行。
file = open("main.py", "r")
print(len(file.readlines()))
print(len(file.read()))