编程一小白 发表于 2017-2-24 17:39:28

os.curdir与os.getcwd ( )有何区别

for each_file in os.listdir (os.curdir ):

print( os.getdir() + os.sep + each_file)

这两个语句中的 os.curdir和 os.getcwd ( ) 能不能通用?

os.curdir是指代当前目录,os.getcwd ( )是返回当前目录,这两者有什么区别,在一般的用法上有何不同?

Santer 发表于 2017-2-24 19:18:46

首先,没有os.getdir() 只有os.getcwd()
其次print()这一句应该这样写:
print((os.getcwd() + os.sep + each_file))
是可以运行的:
C:\Users\Administrator\Pictures\Saved Pictures\捕获.PNG
       

编程一小白 发表于 2017-2-25 15:18:15

32269100 发表于 2017-2-24 17:54
os.getcwd : 得到当前python脚本工作的目录路径。

os.curdir : 获得当前工作目录的字符串名称

不好意思,不是很明白,这二者有什么区别么

elias 发表于 2018-7-10 01:39:39

我看了,两者没啥区别
os.listdir(os.curdir)
os.listdir(os.getcwd())
得出来的结果是一样的

Python.爱好者 发表于 2018-7-10 16:32:19

区别就是一个是变量一个是函数!

Miracle-s 发表于 2018-7-10 22:42:28

如图:os.curdir   是个变量。是个相对路径
          os.getcwd()是函数,返回当前目录的绝对路径

张永强hhhh 发表于 2020-6-29 23:43:52

os.getcwd()会返回绝对路径
os.curdir是返回当前路径,print出来会是个点

hrp 发表于 2020-6-30 00:48:18

本帖最后由 hrp 于 2020-6-30 07:06 编辑

页: [1]
查看完整版本: os.curdir与os.getcwd ( )有何区别