检查Python中是否存在文件

雨点打透心脏的1/2处 2023-03-05 09:59 151阅读 0赞

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())

  1. -bash-4.2$ ls
  2. python_samples test.txt
  3. -bash-4.2$ python3
  4. Python 3.6.8 (default, Apr 25 2019, 21:02:35)
  5. [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
  6. Type "help", "copyright", "credits" or "license" for more information.
  7. >>> import os
  8. >>> from os import path
  9. >>> print(path.exists('test.txt'))
  10. True
  11. >>> print(path.exists('test1.txt'))
  12. False
  13. >>>

os.path.isfile() (os.path.isfile())

  1. -bash-4.2$ ls
  2. python_samples test.txt
  3. -bash-4.2$ python3
  4. Python 3.6.8 (default, Apr 25 2019, 21:02:35)
  5. [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
  6. Type "help", "copyright", "credits" or "license" for more information.
  7. >>> import os
  8. >>> print(os.path.isfile('test.txt'))
  9. True
  10. >>> print(os.path.isfile('test1.txt'))
  11. 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)中也可用。 但是,python 3.4提供了一个函数pathlibPath.exists() ,该函数是从pathlib模块导入的,用于处理文件系统路径。 它使用一种面向对象的方法来验证文件是否存在。

  1. -bash-4.2$ ls
  2. python_samples test.txt
  3. -bash-4.2$ python3
  4. Python 3.6.8 (default, Apr 25 2019, 21:02:35)
  5. [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
  6. Type "help", "copyright", "credits" or "license" for more information.
  7. >>> import pathlib
  8. >>> test_file = 'test.txt'
  9. #create a file object
  10. >>> file = pathlib.Path(test_file)
  11. >>> if file.exists():
  12. ... print("file {} exists".format(test_file))
  13. ... else:
  14. ... print("file {} does not exists".format(test_file))
  15. ...
  16. file test.txt exists
  17. -bash-4.2$ ls
  18. python_samples test.txt
  19. -bash-4.2$ python3
  20. Python 3.6.8 (default, Apr 25 2019, 21:02:35)
  21. [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
  22. Type "help", "copyright", "credits" or "license" for more information.
  23. >>> import pathlib
  24. >>> test_file = 'test1.txt'
  25. >>> file = pathlib.Path(test_file)
  26. >>> if file.exists():
  27. ... print("file {} exists".format(test_file))
  28. ... else:
  29. ... print("file {} does not exists".format(test_file))
  30. ...
  31. file test1.txt does not exists
  32. >>>

简而言之 (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()方法打开文件。

  1. -bash-4.2$ ls
  2. python_samples test.txt
  3. -bash-4.2$ python3
  4. Python 3.6.8 (default, Apr 25 2019, 21:02:35)
  5. [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
  6. Type "help", "copyright", "credits" or "license" for more information.
  7. >>> try:
  8. ... open('text.txt')
  9. ... except:
  10. ... print("file does not exists")
  11. ...
  12. file does not exists
  13. -bash-4.2$ ls
  14. python_samples test.txt
  15. -bash-4.2$ python3
  16. Python 3.6.8 (default, Apr 25 2019, 21:02:35)
  17. [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux
  18. Type "help", "copyright", "credits" or "license" for more information.
  19. >>> file_name = 'test.txt'
  20. >>> try:
  21. ... with open(file_name) as f:
  22. ... print("{} exists".format(file_name))
  23. ... except:
  24. ... print("{} does not exists".format(file_name))
  25. ...
  26. test.txt exists
  27. >>>

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

发表评论

表情:
评论列表 (有 0 条评论,151人围观)

还没有评论,来说两句吧...

相关阅读