首页 > 解决方案 > 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()

标签: pythonwindowscharacter

解决方案


推荐阅读