Home  >  Article  >  Backend Development  >  How to determine whether txt file exists in python

How to determine whether txt file exists in python

尚
Original
2019-07-01 16:03:448506browse

How to determine whether txt file exists in python

Usually before reading or writing a file, you need to determine whether the file or directory exists, otherwise certain processing methods may cause program errors. So it is best to determine whether the file exists before doing any operation.

Here will introduce three methods to determine whether a file or folder exists, using the os module, Try statement, and pathlib module respectively.

1. Use the os module

The os.path.exists() method in the os module is used to check whether the file exists.

Judge whether the file exists import os

import os
os.path.exists(test_file.txt)
#True
os.path.exists(no_exist_file.txt)
#False

2. Use the try statement

You can directly use the open() method in the program to check whether the file exists and is readable and writable.

Syntax:

open()

If the file you open does not exist, the program will throw an error, use the try statement to catch this error.

The program cannot access the file. There may be many reasons:

If the file you open does not exist, a FileNotFoundError exception will be thrown;

The file exists, but there is no permission. Access will throw a PersmissionError exception.

So you can use the following code to determine whether the file exists:

try:
    f =open()
    f.close()
except FileNotFoundError:
    print "File is not found."
except PersmissionError:
    print "You don't have permission to access this file."

In fact, there is no need to handle each exception in such detail. The above two exceptions are subclasses of IOError. So you can simplify the program:

try:
    f =open()
    f.close()
except IOError:
    print "File is not accessible."

Use the try statement to make judgments and handle all exceptions very simply and elegantly. And compared with others, there is no need to introduce other external modules.

3. Use the pathlib module

The pathlib module is a built-in module in the Python3 version, but in Python2, third-party modules need to be installed separately.

Using pathlib requires first using the file path to create a path object. This path can be a file name or a directory path.

Check whether the path exists

path = pathlib.Path("path/file")
path.exist()

Check whether the path is a file

path = pathlib.Path("path/file")
path.is_file()

For more Python-related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of How to determine whether txt file exists in python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn