python - os.walk() 在 Windows 10 上为带有重音字母的文件生成无效路径
问题描述
我正在尝试运行一个返回文件夹总大小及其所有内容的函数。这是功能:
#!/usr/local/bin/python
# -*- coding: utf-8-*-
import os
def get_size(start_path = '.'):
total_size = 0
for dirpath, dirnames, filenames in os.walk(start_path):
for f in filenames:
fp = os.path.join(dirpath, f)
# skip if it is symbolic link
if not os.path.islink(fp):
total_size += os.path.getsize(fp)
return total_size
该函数适用于任何路径作为输入,除非我给它一个文件夹的路径,该文件夹的名称上有一个带有重音字母的文件。
我收到此错误:
Traceback (most recent call last):
File "C:/Users/Owner/PycharmProjects/test/tester.py", line 18, in <module>
print get_size("c:\\nizan\\kal")
File "C:/Users/Owner/PycharmProjects/test/tester.py", line 13, in get_size
total_size += os.path.getsize(fp)
File "C:\Python27\lib\genericpath.py", line 49, in getsize
return os.stat(filename).st_size
WindowsError: [Error 123] The filename, directory name, or volume label
syntax is incorrect: 'c:\\nizan\\kal\\?xample.txt'
Process finished with exit code 1
导致问题的文件的真实名称是“éxample.txt”。我尝试过使用此处所示的解码。但它没有用。
解决方案
推荐阅读
- sql-server - 如何使用 CASE 语句计算 SQL Server 中的存储桶值
- php - 解析链接并替换为 http://
- android - 如何使用动画将视图从屏幕外移动到屏幕底部?
- android - 在 Android 日期选择器中选择值后无法找到其他元素
- android - 更新到 Android 3.1.3 时找不到符号 R
- python - python mysql日期错误“引发errorclass,errorvalue”
- javascript - 当我包含手风琴时,我发现它总是默认为第一个面板
- c# - xamarin 常用模板元素
- tensorflow - 如何在 Tensorflow 或 Keras 中实现 K-Max 池化?
- http - 当我尝试 a2ensite 一个 ssl.conf 文件时,服务器停止工作