How does Python determine whether a file or folder exists?

Source: Internet
Author: User

How does Python determine whether a file or folder exists?

This article mainly describes how to determine whether a file or folder exists in Python. This article also describes how to determine whether a file or folder exists, and how to determine whether it is a file or directory. path. use lexist to determine whether a file or directory exists in FTP. For more information, see

1. python checks whether files and folders exist and creates 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

Ii. python checks whether a 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

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

>>>

If the returned value is True or not, False is returned.

Iv. Determine whether a folder exists in python

The Code is as follows:

$ 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

>>>

5. Check whether the file exists and whether the path is a file in python.

Before writing a file, you usually need to check whether the file path is writable:

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

You can also implement the following method:

The Code is as follows:

Def file_exists (filename ):

Try:

With open (filename) as f:

Return True

Handle t IOError:

Return False

6. python checks whether files and folders exist.

The Code is as follows:

Import OS

OS .path.isfile('test.txt ') # returns False if it does not exist.

OS. path. exists (directory) # If the directory does not exist, False is returned.

VII. OS. path. lexist

And OS. path. lexists (path)

True is also returned for the link file of broken.

8. Determine whether a folder exists through python FTP

How does python determine whether a folder exists? The majority of users gave the answer:

You can use the ftp library. The following is an example of Python core programming:

The Code is as follows:

>>> From ftplib import FTP

>>> F = FTP ('ftp .python.org ')

>>> F. login ('anonus us', 'guido @ python.org ')

'2017 Guest login OK, access restrictions apply .'

>>> F. dir ()

The file does not exist in the dir result.

Or:

The Code is as follows:

Try:

F. retrbinary ('retr % s' % FILE, open (FILE, 'wb '). write)

Failed t ftplib. error_perm:

Print 'error: cannot read file "% s" '% FILE 40 OS. unlink (FILE)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.