Directory
- 1. Using the OS module
- Determine if a file can be read or written
- 2. Using the Try Statement
- 3. Using the Pathlib module
Body
It is usually necessary to determine whether a file or directory exists before reading or writing the file, or some processing may cause the program to fail. So it's best to determine if the file exists before doing anything.
Here are three ways to determine if a file or folder exists, respectively, and os模块
Try语句
pathlib模块
.
1. Using the OS module
The methods in the OS module are os.path.exists()
used to verify that the file exists.
- Determine if a file exists
import osos.path.exists(test_file.txt)#Trueos.path.exists(no_exist_file.txt)#False
- Determine if a folder exists
import osos.path.exists(test_dir)#Trueos.path.exists(no_exist_dir)#False
You can see os.path.exists()
how the File and folder are the same.
In fact, there is a problem with this approach, assuming you want to check whether the file "Test_data" exists, but there is a folder called "Test_data" under the current path, so there may be a miscarriage of error. To avoid such a situation, you can:
By this method, if the file "Test-data" does not exist, it will return false, and vice versa returns True.
That is, the file exists, and you may also need to determine whether the file can be read or written.
Determine if a file can be read or written
Use os.access()
the method to determine whether a file can read or write.
Grammar:
Os.access (path, mode)
Path is the file path, mode is the operating pattern, there are several:
Os. F_OK: Check whether the file exists;
Os. R_OK: Check whether the file is readable;
Os. W_OK: Check whether the file can be written;
Os. X_OK: Checks if the file can be executed
The method returns TRUE or false by judging whether the file path exists and the permissions of the various access modes.
import osif os.access("/file/path/foo.txt", os.F_OK): print "Given file path is exist."if os.access("/file/path/foo.txt", os.R_OK): print "File is accessible to read"if os.access("/file/path/foo.txt", os.W_OK): print "File is accessible to write"if os.access("/file/path/foo.txt", os.X_OK): print "File is accessible to execute"
2. Using the Try Statement
You can use the method directly in your program open()
to check that the file exists and is readable and writable.
Grammar:
Open ()
If your open file does not exist, the program throws an error and uses a try statement to catch the error.
The program cannot access the file, there may be many reasons:
If you open the file does not exist, will throw an FileNotFoundError
exception;
The file exists, but no access is granted, and an exception is thrown PersmissionError
.
So you can use the following code to determine whether a 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 deal with each exception in such detail, the two exceptions above are IOError
subclasses. So you can simplify the program:
try: f =open() f.close()except IOError: print "File is not accessible."
It is very simple and elegant to handle all exceptions using the Try statement. And there is no need to introduce other external modules compared to others.
3. Using the Pathlib module
The Pathlib module is a built-in module in the Python3 version, but in Python2 it is necessary to install the three-party module separately.
Using pathlib requires a file path to create the path object first. This path can be a file name or a directory path.
path = pathlib.Path("path/file")path.exist()
- Check if the path is a file
path = pathlib.Path("path/file")path.is_file()
Python method for judging whether a file exists