统计一个文件中空行的行数。python
时间: 2024-05-14 21:13:36 浏览: 18
可以使用以下代码来统计一个文件中空行的行数:
```python
with open('filename.txt', 'r') as f:
count = 0
for line in f:
if line.strip() == '':
count += 1
print('空行的行数为:', count)
```
其中,`filename.txt`是要统计的文件名。`count`变量用来记录空行的行数,`line.strip()`用来去除行末的换行符和空格,如果去除后为空字符串,则说明这是一个空行。
相关问题
统计 python 源代码文件中代码行数,去除注释,空行,进行输出?
可以使用以下代码来统计 Python 源代码文件中的代码行数,去除注释和空行:
```python
import os
def count_code_lines(file_path):
lines = 0
with open(file_path, 'r') as f:
is_comment = False
for line in f:
line = line.strip()
if line.startswith('"""') or line.startswith("'''"):
is_comment = not is_comment
elif is_comment or line.startswith('#') or not line:
continue
else:
lines += 1
return lines
# 指定目录
dir_path = 'path/to/directory'
# 遍历目录下所有 py 文件并统计代码行数
total_lines = 0
for root, dirs, files in os.walk(dir_path):
for file in files:
if file.endswith('.py'):
file_path = os.path.join(root, file)
lines = count_code_lines(file_path)
total_lines += lines
print(f'{file_path} : {lines}')
print(f'Total lines of code: {total_lines}')
```
代码解释:
1. 定义一个函数 `count_code_lines`,输入为文件路径,输出为该文件中的代码行数。
2. 在 `count_code_lines` 函数中,使用 `with open` 打开文件,并遍历文件中的每一行。
3. 使用一个布尔变量 `is_comment` 来记录当前是否在注释中,如果在注释中,则跳过当前行。
4. 使用 `strip` 方法去除每行开头和结尾的空格和换行符。
5. 判断当前行是否是注释或空行,如果是,则跳过当前行,否则代码行数加一。
6. 在主程序中,使用 `os.walk` 遍历指定目录下的所有文件和子目录。
7. 对于每个以 `.py` 结尾的文件,调用 `count_code_lines` 函数来统计代码行数,并累加到总行数中。
8. 输出每个文件的代码行数和总行数。
Python编写一个程序,功能是统计一个源程序文件的代码行数
好的,以下是一个简单的 Python 程序,实现统计源程序文件的代码行数:
```python
import os
def count_lines(file_path):
with open(file_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
count = len(lines)
for line in lines:
line = line.strip()
if not line or line.startswith('#'):
count -= 1
return count
if __name__ == '__main__':
file_path = input('请输入源程序文件路径:')
if not os.path.exists(file_path):
print('文件不存在')
elif not os.path.isfile(file_path):
print('请指定一个文件')
else:
print(f'源程序文件 {file_path} 共有 {count_lines(file_path)} 行代码')
```
程序中,count_lines 函数用来统计文件的代码行数,排除空行和注释行。程序通过 input 函数获取用户输入的源程序文件路径,使用 os.path 模块检查文件是否存在,并且是一个文件。最后输出统计结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)