Python – 检查文件或目录是否存在
有时需要检查目录或文件是否存在变得很重要,因为您可能想防止覆盖已经存在的文件,或者您想在加载文件之前确保文件可用或不可用。
有多种方法可以检查文件或目录是否已经存在。他们是 -
- 使用 os.path.exists()
- 使用 os.path.isfile()
- 使用 os.path.isdir()
- 使用 pathlib.Path.exists()
使用 os.path.exists()
Python中的OS 模块提供了与操作系统交互的功能。操作系统属于 Python 的标准实用程序模块。该模块提供了一种使用操作系统相关功能的可移植方式。 os.path模块是Python中OS 模块的子模块,用于常见的路径名操作。
注意:要了解有关操作系统模块的更多信息,请单击此处。
Python中的 os.path.exists() 方法用于检查指定路径是否存在。此方法还可用于检查给定路径是否引用打开的文件描述符。
Syntax: os.path.exists(path)
Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a Boolean value of class bool. This method returns True if path exists otherwise returns False.
例子:
Python3
# Python program to explain os.path.exists() method
# importing os module
import os
# Specify path
path = '/usr/local/bin/'
# Check whether the specified
# path exists or not
isExist = os.path.exists(path)
print(isExist)
# Specify path
path = '/home/User/Desktop/file.txt'
# Check whether the specified
# path exists or not
isExist = os.path.exists(path)
print(isExist)
Python3
# Python program to explain os.path.isfile() method
# importing os module
import os
# Path
path = 'C:/Users/gfg/Desktop/file.txt'
# Check whether the
# specified path is
# an existing file
isFile = os.path.isfile(path)
print(isFile)
# Path
path = '/home/User/Desktop/'
# Check whether the
# specified path is
# an existing file
isFile = os.path.isfile(path)
print(isFile)
Python3
# Python program to explain os.path.isdir() method
# importing os.path module
import os.path
# Path
path = '/home/User/Documents/file.txt'
# Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
# Path
path = '/home/User/Documents/'
# Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
Python3
# Python program to explain os.path.isdir() method
# importing os.path module
import os.path
# Create a directory
# (in current working directory)
dirname = "GeeksForGeeks"
os.mkdir(dirname)
# Create a symbolic link
# pointing to above directory
symlink_path = "/home/User/Desktop/gfg"
os.symlink(dirname, symlink_path)
path = dirname
# Now, Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
path = symlink_path
# Check whether the
# specified path (which is a
# symbolic link ) is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
Python3
# Import Path class
from pathlib import Path
# Path
path = '/home/gfg/Desktop'
# Instantiate the Path class
obj = Path(path)
# Check if path points to
# an existing file or directory
print(obj.exists())
输出:
True
False
使用 os.path.isfile()
Python中的 os.path.isfile() 方法用于检查指定路径是否为现有的常规文件。
Syntax: os.path.isfile(path)
Parameter:
path: A path-like object representing a file system path. A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a Boolean value of class bool. This method returns True if specified path is an existing regular file, otherwise returns False.
例子:
Python3
# Python program to explain os.path.isfile() method
# importing os module
import os
# Path
path = 'C:/Users/gfg/Desktop/file.txt'
# Check whether the
# specified path is
# an existing file
isFile = os.path.isfile(path)
print(isFile)
# Path
path = '/home/User/Desktop/'
# Check whether the
# specified path is
# an existing file
isFile = os.path.isfile(path)
print(isFile)
输出:
True
False
使用 os.path.isdir()
Python中的 os.path.isdir() 方法用于检查指定路径是否为现有目录。此方法遵循符号链接,这意味着如果指定的路径是指向目录的符号链接,则该方法将返回 True。
Syntax: os.path.isdir(path)
Parameter:
path: A path-like object representing a file system path.
Return Type: This method returns a Boolean value of class bool. This method returns True if specified path is an existing directory, otherwise returns False.
例子:
Python3
# Python program to explain os.path.isdir() method
# importing os.path module
import os.path
# Path
path = '/home/User/Documents/file.txt'
# Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
# Path
path = '/home/User/Documents/'
# Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
输出:
False
True
示例:如果指定的路径是符号链接。
Python3
# Python program to explain os.path.isdir() method
# importing os.path module
import os.path
# Create a directory
# (in current working directory)
dirname = "GeeksForGeeks"
os.mkdir(dirname)
# Create a symbolic link
# pointing to above directory
symlink_path = "/home/User/Desktop/gfg"
os.symlink(dirname, symlink_path)
path = dirname
# Now, Check whether the
# specified path is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
path = symlink_path
# Check whether the
# specified path (which is a
# symbolic link ) is an
# existing directory or not
isdir = os.path.isdir(path)
print(isdir)
输出:
True
True
使用 pathlib.Path.exists()
Python中的Pathlib 模块提供了各种表示文件系统路径的类,这些类具有适用于不同操作系统的语义。该模块属于 Python 的标准实用程序模块。 Pathlib 模块中的路径类分为纯路径和具体路径。纯路径仅提供计算操作但不提供 I/O 操作,而从纯路径继承的具体路径提供计算操作和 I/O 操作。
注意:要了解有关 pathlib 模块的更多信息,请单击此处。
pathlib.Path.exists() 方法用于检查给定路径是否指向现有文件或目录。
Syntax: pathlib.Path.exists(path)
Parameter:
path: A path-like object representing a file system path.
Return Type: This method returns a Boolean value of class bool. This method returns True if path exists otherwise returns False.
例子:
Python3
# Import Path class
from pathlib import Path
# Path
path = '/home/gfg/Desktop'
# Instantiate the Path class
obj = Path(path)
# Check if path points to
# an existing file or directory
print(obj.exists())
输出:
True