Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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
新Unix用户的简单Python列表_Python_Unix - Fatal编程技术网

新Unix用户的简单Python列表

新Unix用户的简单Python列表,python,unix,Python,Unix,这可能看起来很愚蠢,但我花了很长时间试图编写一个Python 2.6函数,该函数采用目录路径并以字节为单位打印所有文件的大小,而忽略子目录。我还希望按大小反向排序,如以下格式: 文件A:50000 档案乙:40000 文件C:30000 档案-总数:120000 有人可以帮忙吗?稍微修改一下,我们可以得到一个包含两个元组的列表,其中包含(文件名、文件大小) import os all_files = list() dir_path = "." # replace with actual di

这可能看起来很愚蠢,但我花了很长时间试图编写一个Python 2.6函数,该函数采用目录路径并以字节为单位打印所有文件的大小,而忽略子目录。我还希望按大小反向排序,如以下格式:

文件A:50000

档案乙:40000

文件C:30000

档案-总数:120000

有人可以帮忙吗?

稍微修改一下,我们可以得到一个包含两个元组的列表,其中包含(文件名、文件大小)

import os

all_files = list()

dir_path = "." # replace with actual dir path

for file in os.listdir(dir_path):
    path = os.path.join(dir_path, file)
    if os.path.isfile(path):
        all_files.append(tuple([file, os.path.getsize(path)]))

all_files.sort(key = lambda elm: elm[1])

for item in all_files:
    print(item)
你会这样称呼它:

dirname = os.getcwd()
get_files_by_file_size(dirname,reverse=True)

你可以使用
os.listdir
os.path.isfile
os.path.getsize
你真是太棒了,非常感谢你,还有那些澄清的评论!
dirname = os.getcwd()
get_files_by_file_size(dirname,reverse=True)