os.path.isdir(path)异常

Window 10家庭中文版,Python 3.6.4,

当一个路径以多个斜杠(/)或反斜杠字符(\\)结尾时,os.path.isdir(path)函数仍然将它们判断为目录:

>>> os.path.isdir(‘C:/Python36/Lib/sqlite3//‘)
True
>>> os.path.isdir(‘C:/Python36/Lib/sqlite3///‘)
True
>>> os.path.isdir(‘C:/Python36/Lib/sqlite3///////‘)
True
>>> os.path.isdir(‘C:\\Python36\\Lib\\sqlite3\\\\\\\\‘) # 8个反斜杠(转义后是4个反斜杠 )
True

孤的判断是返回False的,为何会如此?后续会继续dig~

----

Python官文信息:

os.path.isdir(path)
Return True if path is an existing directory. This follows symbolic links, so both islink() and isdir() can be true for the same path.

path-like object
An object representing a file system path. A path-like object is either a str or bytes object representing a path, or an object implementing the os.PathLike protocol. An object that supports the os.PathLike protocol can be converted to a str or bytes file system path by calling the os.fspath() function; os.fsdecode() and os.fsencode() can be used to guarantee a str or bytes result instead, respectively. Introduced by PEP 519.

class os.PathLike
An abstract base class for objects representing a file system path, e.g. pathlib.PurePath.

PEP 519 -- Adding a file system path protocol

原文地址:https://www.cnblogs.com/luo630/p/9220225.html

时间: 2024-08-04 19:08:55

os.path.isdir(path)异常的相关文章

os模块 os.stat('path/filename') os.path.dirname(path) os.path.exists(path)  os.path.join(path1[, path2[, ...]])

提供对操作系统进行调用的接口 1 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 2 os.chdir("dirname")  改变当前脚本工作目录:相当于shell下cd 3 os.curdir  返回当前目录: ('.') 4 os.pardir  获取当前目录的父目录字符串名:('..') 5 os.makedirs('dirname1/dirname2')    可生成多层递归目录 6 os.removedirs('dirname1')    若

Python:关于os.path.isdir,os.path.exists,os.walk无法识别“~/" HOME目录的问题。

在编写Python脚本的时候,我发现,os.path.isdir,os.path.exists,os.walk 根本无法识别 ‘~/‘ 表示的HOME目录.例如: Python 2.7.12 (default, Jul 18 2016, 10:55:51) [GCC 6.1.1 20160621 (Red Hat 6.1.1-3)] on linux2 Type "help", "copyright", "credits" or "li

python中os.path.isdir()等函数的作用和用法

一 用法和概念: Python中的os模块用于和系统进行交互,其中: 1 os.listdir()用于返回一个由文件名和目录名组成的列表,需要注意的是它接收的参数需要是一个绝对的路径. 2 os.path.isdir()用于判断对象是否为一个目录. 3 os.path.isfile()用于判断对象是否为一个文件. 二 实例和讲解: 下面看一下他们的用法实例: 路径下的目录和文件: dir_test os_file.py test test.txt 程序代码: 1 import os 2 path

Python os模块,path模块

os模块中关于文件/目录常用的函数使用方法 getcwd() 返回当前工作目录chdir(path) 改变工作目录listdir(path='.') 列举指定目录中的文件名('.'表示当前目录,'..'表示上一级目录)mkdir(path) 创建单层目录,如该目录已存在抛出异常makedirs(path) 递归创建多层目录,如该目录已存在抛出异常,注意:'E:\\a\\b'和'E:\\a\\c'并不会冲突remove(path) 删除文件rmdir(path) 删除单层目录,如该目录非空则抛出异

os.path.exists(path) 和 os.path.lexists(path) 的区别

使用os.path.exists()方法可以直接判断文件是否存在.代码如下:>>> import os>>> os.path.exists(r'C:\1.TXT')False>>> os.path.exists(path)Return True if path refers to an existing path. Returns False for broken symbolic links. On some platforms, this func

os模块之path

os模块 :对文件和路径的批量操作 os.getcwd(): 函数得到当前工作目录,即当前Python脚本工作的目录路径(绝对路径) os.listdir():输出当前路径下的所有文件 (以列表的形式) 1 >>> import os 2 >>> os.getcwd() 3 'F:\\Projects\\Python' 4 >>> os.listdir(os.getcwd()) 5 ['f1040.pdf', 'f1040a.pdf', 'f1040e

InstallShield Build Error -1014: Cannot rename directory <PATH> to <PATH>\folder.Bak.

InstallSheild执行Build结果错误: 错误详细信息: Cannot rename directory <PATH> to <PATH>\folder.Bak. Windows Explorer or a DOS prompt may be pointing to a subfolder of the release output folder (Disk1) or to the Interim folder, locking it. Change the curren

[LeetCode#110, 112, 113]Balanced Binary Tree, Path Sum, Path Sum II

Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Pr

Path Sum,Path Sum II

Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below