python core programming: Python files and folders to determine whether the method exists

This article describes how to determine Python file and folder exists, this paper also explained the method to determine whether a file or directory, the role of os.path.lexist, FTP the file or directory to determine whether there are other content, you need friends can refer to the following

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

Two, python determine whether a file exists

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

Third, how to judge whether there is a Python file

Use os.path.exists () method can be directly determined whether the file exists.

code show as below:

>>> import os
>>> os.path.exists(r'C:\1.TXT')
False
>>>

If there is the return value is True, False is returned if there is no
four, python determine whether there is a folder

$ python
Python 2.7.3 (default, Jan  2 2013, 16:53:07)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>>
>>>
>>> tobecheckdir = r'/home/tim/workspace'
>>> os.path.isdir(tobecheckdir)
True
>>>

Five, python check the file exists and whether the file path

Before writing files typically need to check that the file path can be written:

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 is not readable"

You can also be achieved in the following way:

def file_exists(filename):
    try:
        with open(filename) as f:
            return True
    except IOError:
        return False

Six, python files and folders to determine whether there is

import os
os.path.isfile('test.txt') #如果不存在就返回False
os.path.exists(directory) #如果目录不存在就返回False

Seven, os.path.lexist

There os.path.lexists (path)
of the broken link file returns True.

Eight, python FTP folder to determine whether there is

How python judgment folder exists? The majority of users gives the answer:
use ftp library on it, the following is an example of the core Python Programming:

>>> from ftplib import FTP
>>> f = FTP('ftp.python.org')
>>> f.login('anonymous', '[email protected]')
'230 Guest login ok, access restrictions apply.'
>>> f.dir()

dir result in no such file is not present.
Or as follows:

try:
f.retrbinary('RETR %s' % FILE,open(FILE, 'wb').write)
except ftplib.error_perm:
print 'ERROR: cannot read file "%s"' % FILE 40 os.unlink(FILE)

You can not read this file, but also considered as non-existent.
We recommend the python learning sites , to see how old the program is to learn! From basic python script, reptiles, django, data mining, programming techniques, work experience, as well as senior careful study of small python partners to combat finishing zero-based information projects! Every day, Python programmers explain the timing of technology, sharing some learning methods and the need to pay attention to small details,

发布了51 篇原创文章 · 获赞 122 · 访问量 8万+

Guess you like

Origin blog.csdn.net/haoxun03/article/details/104348620