检查Python中是否存在文件
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')) FalseThe 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)中也可用。 但是,python 3.4提供了一個函數pathlibPath.exists() ,該函數是從pathlib模塊導入的,用于處理文件系統路徑。 它使用一種面向對象的方法來驗證文件是否存在。
-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 = 'test.txt' #create a file object >>> file = 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 = 'test1.txt' >>> file = 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.
除了上述方法外,還有另一種簡單的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('text.txt') ... 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 = 'test.txt' >>> 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
總結
以上是生活随笔為你收集整理的检查Python中是否存在文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java类类getPackage()方法
- 下一篇: ups一直响是什么原因_UPS的完整形式