python macos listdir_Python os.listdir()用法及代码示例
python中的os.listdir()方法用于获取指定目录中所有文件和目录的列表。如果我们未指定任何目录,则将返回当前工作目录中的文件和目录列表。
用法: os.listdir(path)
参数:
路径(可选):目录的路径
返回类型:此方法返回指定路径中所有文件和目录的列表。此方法的返回类型为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的输出相同。所以如果我们省略path参数os.listdir()方法将返回当前工作目录中所有文件和目录的列表。
python macos listdir_Python os.listdir()用法及代码示例相关推荐
- python中的os abort_Python os.abort()用法及代码示例
Python中的OS模块提供了与操作系统进行交互的功能.操作系统属于Python的标准实用程序模块.该模块提供了使用依赖于操作系统的功能的便携式方法. os.abort()Python中的方法用于生成 ...
- python chmod_Python os.chmod用法及代码示例
Python中的os.chmod()方法用于将路径模式更改为数字模式. 用法: os.chmod(path, mode) 参数: path - path name of the file or dir ...
- python getostime_Python os.getrandom()用法及代码示例
Python中的OS模块提供了与操作系统进行交互的功能.操作系统属于Python的标准实用程序模块.该模块提供了使用依赖于操作系统的功能的便携式方法. os.getrandom()方法用于生成适合加密 ...
- python max()_Python Decimal max()用法及代码示例
Decimal#max():max()是一个Decimal类方法,该方法比较两个Decimal值并返回两个最大值. 用法: Decimal.max() 参数: Decimal values 返回: t ...
- python中weekday_Python calendar firstweekday()用法及代码示例
日历模块允许输出类似于程序的日历,并提供与日历相关的其他有用功能. "日历"模块中定义的函数和类使用理想化的日历,当前的公历日历在两个方向上都无限期扩展. 在Python中,cal ...
- python中perf_counter_Python time.perf_counter()用法及代码示例
由于时间模块提供了各种与时间有关的功能.因此,有必要导入时间模块,否则会出错,因为时间模块中存在perf_counter()的定义. perf_counter()函数始终以秒为单位返回时间的浮点值.返 ...
- python function gamma_Python math gamma()用法及代码示例
Python用其语言允许进行各种数学运算,这在科学领域具有多种应用.这样的Python产品之一就是内置的gamma()函数,该函数以数字方式计算函数中传递的数字的伽玛值. 用法:math.gamma( ...
- python compare()_Python Decimal compare()用法及代码示例
Decimal#compare():compare()是一个Decimal类方法,它比较两个Decimal值. 用法:Decimal.compare() 参数:十进制值 返回: 1 –如果a> ...
- isdigit函数python 小数,Python Pandas Series.str.isdigit()用法及代码示例
Python是进行数据分析的一种出色语言,主要是因为以数据为中心的Python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Pandas str.isdigit() ...
最新文章
- 转 ofbiz的webservice接口提供(2)-数据类型的局限性
- 不看你都不知道,原来码农的诞生这么不容易
- 正版七日杀服务器存档,七日杀网吧怎么存档 七日杀网吧存档读档方法介绍-游侠网...
- 【opencv】26.图像水平边缘和竖直边缘的算子数学分析
- 简单的docker命令ubuntu系统
- Caffe 增加自定义 Layer 及其 ProtoBuffer 参数
- 基于Verilog的贪吃蛇小游戏设计(附代码)
- 5阶无向完全图_离散数学图论答案
- win10 win7 一键获取TrustedInstaller权限
- Opengl es2.0 学习笔记(九)颜色混合
- source insight同步的时候崩溃_“我在国外,崩溃了一整年。”
- 解决linux 上网速度慢的问题
- NSA互联网公开情报收集指南:迷宫中的秘密·下
- HardFault错误信息分析定位
- 荣耀8viper4android,王者荣耀职业联赛超玩会2-1险胜仙阁 积分紧追sViper
- 第一章 行列式(知识点部分)
- r语言查找是否存在空值_R语言-缺失值判断以及处理
- WTGNet-PlC协议转换网关
- “Why Should I Trust You?”:Explaining the Predictions of Any Classifier 论文笔记
- id returned 1 existed:让人发疯的devc++报错
热门文章
- python练习题目记录46道
- precede和previous_一直在背单词过程中迷茫,死记硬背还是词根词缀?
- JCheckBox 默认选择_GoldWave使用习惯的一些默认设置,学会了吗?
- hadoop错误找不到或无法加载主类
- android多个客户端吗,玩Android客户端已经有100多个开源版本啦
- 蓝牙耳机被网页播放器关闭问题
- 关于this的指向问题(总结)
- java jar 找不到文件_jar包中File 文件找不到的异常分析与解决
- 贾跃亭与恒大“结婚”仨月就闹崩 各执一词谁之过
- 神经网络与图像识别,人脸识别和神经网络