鱼C论坛

 找回密码
 立即注册
查看: 2926|回复: 1

os.getcwd()和os.curdir的区分,google了一下,还有些不明白

[复制链接]
发表于 2017-11-29 17:57:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
如题,看了一下相关内容:
os.getcwd() 获取当前工作目录,即当前python代码工作的目录路径。
os.curdir 返回当前目录: ('.')  ??这个比较难理解。是否有通俗的方式来说说‘.’怎么就能代表当前目录了?类似的os.pardir用‘..’来代替上一层目录。
如果在idle上输入上述命令,可以看到上述命令的返回值不一样。

如果用os.listdir的命令,下述三种表达方式返回值相同:
1.os.listdir(os.getcwd())
2.os.listdir(os.curdir)
3.os.listdir()
用Idle逐个命令输出验证过,应该是等价的吧??


另外,通过help(os.curdir)返回:   #这部分没读懂啊
Attribute references
********************
An attribute reference is a primary followed by a period and a name:

   attributeref ::= primary "." identifier

The primary must evaluate to an object of a type that supports
attribute references, which most objects do.  This object is then
asked to produce the attribute whose name is the identifier.  This
production can be customized by overriding the "__getattr__()" method.
If this attribute is not available, the exception "AttributeError" is
raised.  Otherwise, the type and value of the object produced is
determined by the object.  Multiple evaluations of the same attribute
reference may yield different objects.

Related help topics: getattr, hasattr, setattr, ATTRIBUTEMETHODS, FLOAT,MODULES, OBJECTS

通过help(os.getcwd)返回:   这部分比较易懂
Help on built-in function getcwd in module nt:

getcwd()
    Return a unicode string representing the current working directory.
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-11-29 18:06:55 | 显示全部楼层
又查了一下,在相对路径引用时,可以使用./和../来分别代表当前目录和上一级目录。其中./可以忽略。通俗点,就是系统能识别出这是代表当前的目录,就是这个意思?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-26 04:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表