22 lines
645 B
Python
22 lines
645 B
Python
import os
|
|
|
|
path = "C:/Users/oyyz/Desktop/新建文件夹"
|
|
|
|
total_chars = 0
|
|
# 遍历目录下的所有文件
|
|
for filename in os.listdir(path):
|
|
# 只处理txt文件
|
|
if filename.endswith('.txt'):
|
|
file_path = os.path.join(path, filename)
|
|
try:
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
content = f.read()
|
|
chars = len(content)
|
|
total_chars += chars
|
|
print(f"{filename}: {chars} 个字符")
|
|
except Exception as e:
|
|
print(f"读取文件 {filename} 时出错: {e}")
|
|
|
|
print(f"\n所有txt文件共包含 {total_chars} 个字符")
|
|
|