Python| os.listdir() 方法
Python中的os.listdir()方法用于获取指定目录下所有文件和目录的列表。如果我们不指定任何目录,则将返回当前工作目录中的文件和目录列表。
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.
代码 #1:使用 os.listdir() 方法
# Python program to explain os.listdir() method
# importing os module
import os
# Get the list of all files and directories
# in the root directory
path = "/"
dir_list = os.listdir(path)
print("Files and directories in '", path, "' :")
# print the list
print(dir_list)
输出:
Files and directories in ' / ' :
['sys', 'run', 'tmp', 'boot', 'mnt', 'dev', 'proc', 'var', 'bin', 'lib64', 'usr',
'lib', 'srv', 'home', 'etc', 'opt', 'sbin', 'media']
代码#2:使用 os.listdir() 方法
# Python program to explain os.listdir() method
# importing os module
import os
# Get the path of current working directory
path = os.getcwd()
# Get the list of all files and directories
# in current working directory
dir_list = os.listdir(path)
print("Files and directories in '", path, "' :")
# print the list
print(dir_list)
输出:
Files and directories in ' /home/ihritik ' :
['.rstudio-desktop', '.gnome', '.ipython', '.cache', '.config', '.ssh', 'Public',
'Desktop', '.pki', 'R', '.bash_history', '.Rhistory', '.oracle_jre_usage', 'Music',
'.ICEauthority', 'Documents', 'examples.desktop', '.swipl-dir-history', '.local',
'.gnupg', '.profile', 'Pictures', '.keras', '.viminfo', '.thunderbird', 'Templates',
'.bashrc', '.bash_logout', '.sudo_as_admin_successful', 'Videos', 'images',
'tf_wx_model', 'Downloads', '.mozilla', 'geeksforgeeks']
代码 #3:省略路径参数
# Python program to explain os.listdir() method
# importing os module
import os
# If we do not specify any path
# os.listdir() method will return
# the list of all files and directories
# in current working directory
dir_list = os.listdir()
print("Files and directories in current working directory :")
# print the list
print(dir_list)
输出:
Files and directories in current working directory :
['.rstudio-desktop', '.gnome', '.ipython', '.cache', '.config', '.ssh', 'Public',
'Desktop', '.pki', 'R', '.bash_history', '.Rhistory', '.oracle_jre_usage', 'Music',
'.ICEauthority', 'Documents', 'examples.desktop', '.swipl-dir-history', '.local',
'.gnupg', '.profile', 'Pictures', '.keras', '.viminfo', '.thunderbird', 'Templates',
'.bashrc', '.bash_logout', '.sudo_as_admin_successful', 'Videos', 'images',
'tf_wx_model', 'Downloads', '.mozilla', 'geeksforgeeks']
正如我们所见,代码 #2和代码 #3的输出是相同的。所以如果我们省略路径参数os.listdir()
方法将返回当前工作目录中所有文件和目录的列表。