os.path函数是只可以向下判断吗?
来源:2-3 os模块之os.path
沉默是金Searching
2019-06-12 00:51:53
G:\IT>python
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getcwd()
'G:\\IT'
>>> os.listdir()
['cmd命令.xlsx', 'C语言', 'PyCharm', 'Python', 'Python-project', 'Python学习笔记', '软件设计师教程']
>>> os.path.isdir('IT')
False
>>> os.path.isdir('PyCharm')
True
>>> os.path.isdir('pycharm')
True当前路径在G:\IT,os.path.isdir('IT')返回False,判断IT文件内的文件夹则可以正确判断,所以os.path函数是只可以向下判断吗?
1回答
同学,你好。 os.path.isdir()函数的参数如果只写文件名,则默认为当前路径。该路径下没有IT这个目录,因此返回False。同学可以使用绝对路径进行判断,例:os.path.isdir("G:\IT")
如果我的回答解决了您的疑惑,请采纳!祝学习愉快~~~~
相似问题