This article mainly introduces the python to determine whether files and folders exist methods, this article also explains whether the file or directory of the method, the role of Os.path.lexist, FTP to determine whether the file or directory exists, and other content, the need for friends can refer to the
First, Python to determine whether files and folders exist, create folders
The code is as follows:
>>> Import OS
>>> os.path.exists (' d:/assist ')
True
>>> os.path.exists (' d:/assist/getteacherlist.py ')
True
>>> os.path.isfile (' d:/assist ')
False
>>> os.path.isfile (' d:/assist/getteacherlist.py ')
True
>>> os.makedirs (' D:/assist/set ')
>>> os.path.exists (' D:/assist/set ')
True
Second, Python to determine whether the file exists
The code is as follows:
Import OS
filename = R '/home/tim/workspace/test.txt '
If os.path.exists (filename):
message = ' OK, the '%s ' file exists. '
Else
Message = "Sorry, I cannot find the"%s "file."
Print message% filename
Iii. How to use Python to determine whether a file exists
Use the Os.path.exists () method to directly determine whether a file exists.
The code is as follows:
The code is as follows:
>>> Import OS
>>> os.path.exists (R ' C:1.txt ')
False
>>>
Returns False if there is a return value of true if it does not exist
Four, Python to determine whether the folder exists
The code is as follows:
$ python
Python 2.7.3 (default, 2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type ' help ', ' copyright ', ' credits ' or ' license ' for the more information.
>>> Import OS
>>>
>>>
>>> Tobecheckdir = R '/home/tim/workspace '
>>> Os.path.isdir (Tobecheckdir)
True
>>>
Five, Python check if the file exists, and whether the path is a file
You usually need to check that the file path is writable before you write the file:
The code is as follows:
From OS import path, Access, R_OK # W_OK for write permission.
Path= './file.txt '
If path.exists (path) and Path.isfile (path) and access (path, R_OK):
print ' File exists and is readable '
Else
print ' either file is missing or isn't not readable '
You can also do this in the following ways:
The code is as follows:
def file_exists (filename):
Try
with open (filename) as F:
Return True
Except IOError:
Return False
Six, Python to determine whether files and folders exist
The code is as follows:
Import OS
Os.path.isfile (' Test.txt ') #如果不存在就返回False
Os.path.exists (directory) #如果目录不存在就返回False
Seven, Os.path.lexist
and os.path.lexists (PATH)
The link file for broken also returns true.
Eight, Python ftp to determine whether the folder exists
How does python determine if a folder exists? The majority of netizens gave the answer:
Using the FTP library, here are some examples of Python core programming:
The code is as follows:
>>> from Ftplib import FTP
>>> f = FTP (' ftp.python.org ')
>>> f.login (' Anonymous ', ' guido@python.org ')
' 230 Guest Login OK, access restrictions apply. '
>>> F.dir ()
There is no such file in the dir result.
Or as follows:
The code is as follows:
Try
F.retrbinary (' RETR%s '% file,open (FILE, ' WB '). Write)
Except Ftplib.error_perm:
print ' Error:cannot read file '%s '% file Os.unlink (file)