|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
第30讲os模块,关于os.curdir和os.getcwd()的区别是什么呢?
两个都是获取当前地址,具体怎么使用?
def search_file(start_dir, target) :
os.chdir(start_dir)
for each_file in os.listdir(os.curdir) : #这里为什么用os.curdir呢?
ext = os.path.splitext(each_file)[1]
if ext in target :
vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep) # 这里用os.getcwd()
if os.path.isdir(each_file) :
search_file(each_file, target) # 递归调用
os.chdir(os.pardir) # 递归调用后切记返回上一层目录
本帖最后由 Twilight6 于 2020-7-5 10:42 编辑
os.curdir 和 '.' 都指代当前目录('.'),但是 用 os.curdir 更加标准
os.getcwd() 是获取当前工作目录,如果这里你用 os.curdir 到时候 打印的结果也是显示 . 的,如: .\xxx\xxx\test.py 这样的
而用 os.getcwd() 是打印完整的 如: X:\python\xxx\test.py
看下官方文档描述,更为专业,我们平常使用不用太过在意:
os.curdir的描述不是获取当前目录,而是:
The constant string used by the operating system to refer to the current directory.
操作系统用来引用当前目录的常量字符串。
os.getcwd的描述不是返回当前目录,而是
Return a string representing the current working directory.
返回表示当前工作目录的字符串
|
|