Python - 获取目录中具有大小的文件列表
在本文中,我们将看到如何提取目录的文件列表及其大小。为此,我们将使用 OS 模块。
Python的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。 os.path 模块是Python中 OS 模块的子模块,用于通用路径名操作。
使用的功能
- Python的os.path.isfile()方法用于检查指定路径是否为现有的常规文件。
Syntax: os.path.isfile(path)
Parameter:
- path: path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a Boolean value of class bool. This method returns True if specified path is an existing regular file, otherwise returns False.
- Python的os.path.join()方法智能地连接一个或多个路径组件。此方法连接各种路径组件,并在每个非空部分(最后一个路径组件除外)之后使用一个目录分隔符 ('/')。如果要加入的最后一个路径组件为空,则在末尾放置一个目录分隔符 ('/')。如果路径组件表示绝对路径,则所有先前加入的组件都将被丢弃,并从绝对路径组件继续加入。
Syntax: os.path.join(path, *paths)
Parameter:
- path: A path-like object representing a file system path.
- *path: A path-like object representing a file system path. It represents the path components to be joined.
- A path-like object is either a string or bytes object representing a path.
- Note: The special syntax *args (here *paths) in function definitions in python is used to pass a variable number of arguments to
- a function.
Return Type: This method returns a string which represents the concatenated path components.
- os.listdir():这个 Python的方法用于获取指定目录下所有文件和目录的列表。如果我们不指定任何目录,则将返回当前工作目录中的文件和目录列表。
Syntax: os.listdir(path)
Parameters:
- path (optional) : path of the directory
Return Type: This method returns the list of all files and directories in the specified path. The return type of this method is list.
- 筛选(): 这个 方法在一个函数的帮助下过滤给定的序列,该函数测试序列中的每个元素是否为真。
Syntax: filter(function, sequence)
Parameters:
- function: function that tests if each element of a sequence true or not.
- sequence: sequence which needs to be filtered, it can be sets, lists, tuples, or containers of any iterators.
Returns: returns an iterator that is already filtered.
- os.stat() : Python的这个方法在指定的路径上执行 stat() 系统调用。该方法用于获取指定路径的状态。
Syntax: os.stat(path)
Parameter:
path: A string or bytes object representing a valid path
Returns : st_size: It represents the size of the file in bytes.
- os.walk() :这个 通过自顶向下或自底向上遍历树,在目录树中生成文件名。对于以目录 top 为根的树中的每个目录(包括 top 本身),它产生一个 3 元组(dirpath、dirnames、filenames)。
具有大小的目录中的文件列表
在这部分代码中,我们将只获取文件名称和大小的列表。在这段代码中,我们有 os.stat()函数来获取每个文件的大小,大小将导致 'byte' 所以我们必须将文件的大小从 1024*1024 中除以得到 '兆字节”以便更好地理解。
使用的目录
Python3
# import python modules
import os
# directory name from which
# we are going to extract our files with its size
path = "D:\Books"
# Get list of all files only in the given directory
fun = lambda x : os.path.isfile(os.path.join(path,x))
files_list = filter(fun, os.listdir(path))
# Create a list of files in directory along with the size
size_of_file = [
(f,os.stat(os.path.join(path, f)).st_size)
for f in files_list
]
# Iterate over list of files along with size
# and print them one by one.
for f,s in size_of_file:
print("{} : {}mb".format(f, round(s/(1024*1024),3)))
Python3
# import python modules
import os
# directory name from which we are
# going to extract our files with its size
path = "D:\ABC"
# Get list of all files only in the given directory
fun = lambda x : os.path.isfile(os.path.join(path,x))
files_list = filter(fun, os.listdir(path))
# Create a list of files in directory along with the size
size_of_file = [
(f,os.stat(os.path.join(path, f)).st_size)
for f in files_list
]
# Iterate over list of files along with size
# and print them one by one.
# now we have print the result by
# sorting the size of the file
# so, we have call sorted function
# to sort according to the size of the file
# created a lambda function that help us
# to sort according the size of the file.
fun = lambda x : x[1]
# in this case we have its file path instead of file
for f,s in sorted(size_of_file,key = fun):
print("{} : {}mb".format(os.path.join(path,f),round(s/(1024*1024),3)))
Python3
# import python modules
import os
# directory name from which we are
# going to extract our files with its size
path = "D:\ABC"
# Get list of all files only in the given directory
fun = lambda x : os.path.isfile(os.path.join(path,x))
files_list = filter(fun, os.listdir(path))
# Create a list of files
# in directory along with the size
size_of_file = [
(f,os.stat(os.path.join(path, f)).st_size)
for f in files_list
]
# Iterate over list of files along with size
# and print them one by one.
# now we have print the result by
# sorting the size of the file
# so, we have call sorted function
# to sort according to the size of the file
# created a lambda function that help
# us to sort according the size of the file.
fun = lambda x : x[1]
# in this case we have use its file name.
for f,s in sorted(size_of_file,key = fun):
print("{} : {}mb".format(f,round(s/(1024*1024),3)))
Python3
# import python modules
import os
# directory name from which we are
# going to extract our files with its size
path = "D:\ABC"
# get the path p, sub_directory sub_dir,
# and filename files from the given path
walk_method = os.walk(path)
# using exception handling to remove
# the stop iteration from generator object
# which we get the output from os.walk() method.
while True:
try:
p, sub_dir, files = next(walk_method)
break
except:
break
# Create a list of files in directory along with the size
size_of_file = [
(f,os.stat(os.path.join(path, f)).st_size)
for f in files
]
# get the size of the sub_dir of the given path
for sub in sub_dir:
i = os.path.join(path,sub)
size = 0
for k in os.listdir(i):
size += os.stat(os.path.join(i,k)).st_size
size_of_file.append((sub,size))
# Iterate over list of files along with size
# and print them one by one.
# now we have print the result by
# sorting the size of the file
# so, we have call sorted function
# to sort according to the size of the file
# in this case we have use its file paths.
for f,s in sorted(size_of_file,key = lambda x : x[1]):
print("{} : {}mb".format(os.path.join(path,f),round(s/(1024*1024),3)))
输出:
2015_Book_LinearAlgebraDoneRight.pdf : 2.199mb
An Introduction to Statistical Learning - Gareth James.pdf : 8.996mb
Hand-on-ML.pdf : 7.201mb
ISLR Seventh Printing.pdf : 11.375mb
The Business of the 21st Century - Robert Kiyosaki.pdf : 8.932mb
The Elements of Statistical Learning - Trevor Hastie.pdf : 12.687mb
the_compound_effect_ebook.pdf : 5.142mb
按大小排序的目录中的文件路径列表
在前面的代码中,我们只输出了文件名及其对应的大小,但在这种情况下,我们打印了文件路径而不是每个文件名,并根据每个文件名的大小升序对其进行了排序。在这种情况下,我们必须使用 sorted函数,根据文件大小对文件进行排序。
Syntax: sorted(iterable, key=key, reverse=reverse)
Parameters:
- iterable : The sequence to sort, list, dictionary, tuple etc.
- key : A Function to execute to decide the order. Default is None
reverse : A Boolean. False will sort ascending, True will sort descending. Default is False
使用的目录
蟒蛇3
# import python modules
import os
# directory name from which we are
# going to extract our files with its size
path = "D:\ABC"
# Get list of all files only in the given directory
fun = lambda x : os.path.isfile(os.path.join(path,x))
files_list = filter(fun, os.listdir(path))
# Create a list of files in directory along with the size
size_of_file = [
(f,os.stat(os.path.join(path, f)).st_size)
for f in files_list
]
# Iterate over list of files along with size
# and print them one by one.
# now we have print the result by
# sorting the size of the file
# so, we have call sorted function
# to sort according to the size of the file
# created a lambda function that help us
# to sort according the size of the file.
fun = lambda x : x[1]
# in this case we have its file path instead of file
for f,s in sorted(size_of_file,key = fun):
print("{} : {}mb".format(os.path.join(path,f),round(s/(1024*1024),3)))
输出:
D:\ABC\1.png : 0.022mb
D:\ABC\17.png : 0.024mb
D:\ABC\16.png : 0.036mb
D:\ABC\15.png : 0.047mb
D:\ABC\7.png : 0.074mb
D:\ABC\10.png : 0.076mb
D:\ABC\6.png : 0.09mb
D:\ABC\13.png : 0.093mb
D:\ABC\14.png : 0.097mb
D:\ABC\8.png : 0.104mb
D:\ABC\2.png : 0.115mb
D:\ABC\5.png : 0.126mb
D:\ABC\11.mp4 : 5.966mb
目录中按大小排序的文件名列表:
这段代码与之前的代码没有太大的不同,这段代码有一个很小的变化,在这段代码中,我们只是将输出打印为文件名而不是文件路径,其余代码相同。
使用的目录
蟒蛇3
# import python modules
import os
# directory name from which we are
# going to extract our files with its size
path = "D:\ABC"
# Get list of all files only in the given directory
fun = lambda x : os.path.isfile(os.path.join(path,x))
files_list = filter(fun, os.listdir(path))
# Create a list of files
# in directory along with the size
size_of_file = [
(f,os.stat(os.path.join(path, f)).st_size)
for f in files_list
]
# Iterate over list of files along with size
# and print them one by one.
# now we have print the result by
# sorting the size of the file
# so, we have call sorted function
# to sort according to the size of the file
# created a lambda function that help
# us to sort according the size of the file.
fun = lambda x : x[1]
# in this case we have use its file name.
for f,s in sorted(size_of_file,key = fun):
print("{} : {}mb".format(f,round(s/(1024*1024),3)))
输出:
1.png : 0.022mb
17.png : 0.024mb
16.png : 0.036mb
15.png : 0.047mb
7.png : 0.074mb
10.png : 0.076mb
6.png : 0.09mb
13.png : 0.093mb
14.png : 0.097mb
8.png : 0.104mb
2.png : 0.115mb
5.png : 0.126mb
11.mp4 : 5.966mb
按大小排序的目录和子目录中的文件路径列表:
这段代码不同于以上所有 3 段代码,在这段代码中,我们必须显示所有子目录和文件大小及其名称或文件路径。因此,首先,我们必须使用os.walk()函数获取目录中存在的所有 sub_directores 和文件,这会生成一个包含 3 项内容的生成器对象,即路径、子目录名称和文件名。给定目录。然后我们创建了一个具有大小的文件列表,接下来,我们必须获取目录中存在的 sub_directory 的大小。最后,我们输出了我们的代码,其中包含文件名及其子目录的排序大小。
Syntax: os.walk(path)
Parameters:
path : The path of the directory from where we can create our directory tree.
Returns :
- root : Prints out directories only from what you specified.
- dirs : Prints out sub-directories from root.
- files : Prints out all files from root and directories.
使用的目录
蟒蛇3
# import python modules
import os
# directory name from which we are
# going to extract our files with its size
path = "D:\ABC"
# get the path p, sub_directory sub_dir,
# and filename files from the given path
walk_method = os.walk(path)
# using exception handling to remove
# the stop iteration from generator object
# which we get the output from os.walk() method.
while True:
try:
p, sub_dir, files = next(walk_method)
break
except:
break
# Create a list of files in directory along with the size
size_of_file = [
(f,os.stat(os.path.join(path, f)).st_size)
for f in files
]
# get the size of the sub_dir of the given path
for sub in sub_dir:
i = os.path.join(path,sub)
size = 0
for k in os.listdir(i):
size += os.stat(os.path.join(i,k)).st_size
size_of_file.append((sub,size))
# Iterate over list of files along with size
# and print them one by one.
# now we have print the result by
# sorting the size of the file
# so, we have call sorted function
# to sort according to the size of the file
# in this case we have use its file paths.
for f,s in sorted(size_of_file,key = lambda x : x[1]):
print("{} : {}mb".format(os.path.join(path,f),round(s/(1024*1024),3)))
输出:
D:\ABC\1.png : 0.022mb
D:\ABC\17.png : 0.024mb
D:\ABC\16.png : 0.036mb
D:\ABC\15.png : 0.047mb
D:\ABC\7.png : 0.074mb
D:\ABC\10.png : 0.076mb
D:\ABC\6.png : 0.09mb
D:\ABC\13.png : 0.093mb
D:\ABC\14.png : 0.097mb
D:\ABC\8.png : 0.104mb
D:\ABC\2.png : 0.115mb
D:\ABC\5.png : 0.126mb
D:\ABC\11.mp4 : 5.966mb
D:\ABC\Books : 56.532mb