Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何找到文件夹';Python 2.7中的大小是多少?-一个错误_Python 2.7_Size_Text Files - Fatal编程技术网

Python 2.7 如何找到文件夹';Python 2.7中的大小是多少?-一个错误

Python 2.7 如何找到文件夹';Python 2.7中的大小是多少?-一个错误,python-2.7,size,text-files,Python 2.7,Size,Text Files,我使用以下代码(在Python 2.7.8中)在一个大目录中查找以MB\GB为单位的子文件夹: # -*- coding: cp1255 -*- import os def conv_MB_to_GB(input_megabyte): gigabyte = 1.0/1000/1000 convert_gb = gigabyte * input_megabyte return convert_gb def get_size(start_path =

我使用以下代码(在Python 2.7.8中)在一个大目录中查找以MB\GB为单位的子文件夹:

# -*- coding: cp1255 -*-
import os
def conv_MB_to_GB(input_megabyte):
        gigabyte = 1.0/1000/1000
        convert_gb = gigabyte * input_megabyte
        return convert_gb
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)
            total_size += os.path.getsize(fp)
    return  total_size/(1024*1024.0)
rootPath = r"G:\PROJECTS"
os.chdir(rootPath)
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
for dirs in all_subdirs:
    dir = os.path.join(rootPath, dirs)
    os.chdir(dir)
    current = os.getcwd()
    print current
    if get_size(current) < 1000:
            print "%0.1fMB" % get_size(current)
    if get_size(current) > 1000:
            #print "%0.2fGB" % ((get_size(current))/1000) # this line do the same as the next line
            print "---%0.1fGB---" % (conv_MB_to_GB(get_size(current))*1000)
    print

不幸的是,我无法测试这一点,因为我不是在安装了Python的机器上,但以下更改应该可以工作:

此外,还需要以可识别的格式对原始文件名进行编码—在本例中为utf-8。请注意,这是
encode
的默认参数,因此理论上可以忽略它

有关更多参考资料,请参阅:


我收到一个错误:UnicodeDecodeError:“ascii”编解码器无法解码第9位的字节0xf2:序号不在范围内(128)
WindowsError: [Error 123] ‏‏שם הקובץ,: 'G:\\PROJECTS\\mikta7\\gis\\?\xee\xf1\xee\xea \xe8\xf7\xf1\xe8 ?\xe7\xe3\xf9.txt'
for f in filenames:
    fp = os.path.join(dirpath, f.encode('utf-8'))
    total_size += os.path.getsize(fp)