An ability to check if the file exists or not, is very crucial in any application. Often, the applications perform verifications like,
在任何应用程序中,检查文件是否存在的能力至关重要。 通常,应用程序会执行验证,例如,
Check if the file exists before appending/writing to it.
在追加/写入文件之前检查文件是否存在。
Check if the file exists before reading it.
在读取文件之前,请检查文件是否存在。
The python programming language provides multiple methods to check if the file exists or not. The module which provides the functions as ‘os' , so it is important to import os, while there is a verification on file's existence.
python编程语言提供了多种方法来检查文件是否存在 。 提供功能为'os'的模块,因此在验证文件是否存在的同时导入os非常重要。
os.path.exists() (os.path.exists())
-bash-4.2$ ls
python_samples test.txt
-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> from os import path
>>> print(path.exists('test.txt'))
True
>>> print(path.exists('test1.txt'))
False
>>>
os.path.isfile() (os.path.isfile())
-bash-4.2$ ls
python_samples test.txt
-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.path.isfile('test.txt'))
True
>>> print(os.path.isfile('test1.txt'))
False
The functions, demonstrated above are also available in a lower version of python (<3). However, python version 3.4 provides a function pathlibPath.exists() which is imported from pathlib module for handling the file system path. It uses an object-oriented approach to verify if the file exists or not.
上面演示的功能在较低版本的python(<3)中也可用。 但是&#xff0c;python 3.4提供了一个函数pathlibPath.exists() &#xff0c;该函数是从pathlib模块导入的&#xff0c;用于处理文件系统路径。 它使用一种面向对象的方法来验证文件是否存在。
-bash-4.2$ ls
python_samples test.txt
-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pathlib
>>> test_file &#61; &#39;test.txt&#39;
#create a file object
>>> file &#61; pathlib.Path(test_file)
>>> if file.exists():
... print("file {} exists".format(test_file))
... else:
... print("file {} does not exists".format(test_file))
...
file test.txt exists
-bash-4.2$ ls
python_samples test.txt
-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pathlib
>>> test_file &#61; &#39;test1.txt&#39;
>>> file &#61; pathlib.Path(test_file)
>>> if file.exists():
... print("file {} exists".format(test_file))
... else:
... print("file {} does not exists".format(test_file))
...
file test1.txt does not exists
>>>
简而言之 (In a nutshell)
Use path.exists to verify if the given file exists.
使用path.exists验证给定文件是否存在。
Use path.isfile to check whether a path is file.
使用path.isfile来检查路径是否为文件。
The version Python 3.4 and above provides a pathlib Module to verify the existence of file.
Python 3.4及更高版本提供了一个pathlib模块来验证文件是否存在。
Along with the above-mentioned methods, there is another straight forward pythonic way for checking the existence of the file. Use the open() method to open the file.
除了上述方法外&#xff0c;还有另一种简单的Python方式可以检查文件的存在。 使用open()方法打开文件。
-bash-4.2$ ls
python_samples test.txt
-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> try:
... open(&#39;text.txt&#39;)
... except:
... print("file does not exists")
...
file does not exists
-bash-4.2$ ls
python_samples test.txt
-bash-4.2$ python3
Python 3.6.8 (default, Apr 25 2019, 21:02:35)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> file_name &#61; &#39;test.txt&#39;
>>> try:
... with open(file_name) as f:
... print("{} exists".format(file_name))
... except:
... print("{} does not exists".format(file_name))
...
test.txt exists
>>>
Usage of with in above example, ensures the file is closed after the file operation.
在上面的示例中使用with可以确保在文件操作后关闭文件。
翻译自: https://www.includehelp.com/python/check-if-a-file-exists.aspx