python - Python 中特殊字符和 os.path.getsize() 的问题
问题描述
我正在尝试获取文件夹中所有文件的文件大小。我编写了一个似乎可以工作的脚本,实际上它可以工作,直到我找到一些带有特殊字符的文件,比如“š”。在这种情况下,我收到此错误:
WindowsError: [错误 2] Le fichier spécifié est introuvable: '...Strojni\x9aki...'
(le fichier spécifié est introuvable = 找不到文件)
完整的单词应该是“Strojni š ki”。你能帮助我吗?非常感谢!
下面,我写的代码:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import enum
from os import path, startfile
# Enum for size units
class SIZE_UNIT(enum.Enum):
BYTES = 1
KB = 2
MB = 3
GB = 4
def convert_unit(size_in_bytes, unit):
""" Convert the size from bytes to other units like KB, MB or GB"""
if unit == SIZE_UNIT.KB:
return size_in_bytes/1024
elif unit == SIZE_UNIT.MB:
return size_in_bytes/(1024*1024)
elif unit == SIZE_UNIT.GB:
return size_in_bytes/(1024*1024*1024)
else:
return size_in_bytes
def get_file_size(file_name, size_type = SIZE_UNIT.BYTES ):
""" Get file in size in given unit like KB, MB or GB"""
size = os.path.getsize(file_name)
# return size
return convert_unit(size, size_type)
# start editable vars #
outputfile = "...\\listfile.txt" # file to save the results to
folder = "...\\" # the folder to inventory
exclude = ['Thumbs.db','.tmp'] # exclude files containing these strings
pathsep = "\\" # path seperator ('/' for linux, '\\' for Windows)
# end editable vars #
with open(outputfile, "w") as txtfile:
j=1
for path,dirs,files in os.walk(folder):
if j==1:
sep = "#############################################" + " START LISTING FILES" + " #############################################\n"
else:
sep = "\n \n---------- " + path.split(pathsep)[len(path.split(pathsep))-1] + " ----------\n"
print sep
txtfile.write("%s\n" % sep)
print(path)
txtfile.write(path)
j=j+1
for fn in sorted(files):
if not any(x in fn for x in exclude):
filename = os.path.splitext(fn)[0]
fileext = os.path.splitext(fn)[1]
gg = path + "\\" + filename + fileext
ss = get_file_size(gg, SIZE_UNIT.KB)
print(ss)
ss = str(ss)
sp = (" " + ss + " KB")
print ('{:99s} {:5s} {:7s}'.format(filename, fileext, sp))
txtfile.write('{:99s} {:5s} {:7s}'.format("\n" + filename, fileext, sp))
txtfile.close()
解决方案
推荐阅读
- javascript - 如何使用三个 JS 在魔方上的每个小方块上加载图像
- c# - 在单个 C# 文件中忽略来自全局命名空间的冲突定义
- solr - 停用词和短语查询 solr
- c# - CryptographicException:要解密的数据长度对 playfabserver 无效
- javascript - 如何打开具有下拉效果的菜单?
- python - 将值有效映射到具有相同标签的像素
- qt - QTreeWidget 避免默认选择新项目
- javascript - 粘贴事件在可编辑的内容中未按预期工作
- exception - Dart / Flutter - 调试器在捕获到异常时停止
- scala - 使用列名数组中的 UDF 将列合并到单个映射中