如何使用python识别文件是普通文件还是目录

问题:如何使用python识别文件是普通文件还是目录

如何使用python检查文件是普通文件还是目录?

How do you check whether a file is a normal file or a directory using python?


回答 0

os.path.isdir()并且os.path.isfile()应该给你你想要的东西。请参阅:http//docs.python.org/library/os.path.html

os.path.isdir() and os.path.isfile() should give you what you want. See: http://docs.python.org/library/os.path.html


回答 1

至于其他的答案说,os.path.isdir()os.path.isfile()你想要的东西。但是,您需要记住,这不是仅有的两种情况。使用os.path.islink()的符号链接的实例。此外,False如果文件不存在,这些都将返回,因此您可能还需要检查一下os.path.exists()

As other answers have said, os.path.isdir() and os.path.isfile() are what you want. However, you need to keep in mind that these are not the only two cases. Use os.path.islink() for symlinks for instance. Furthermore, these all return False if the file does not exist, so you’ll probably want to check with os.path.exists() as well.


回答 2

蟒3.4引入pathlib模块到标准库,它提供了一个面向对象的方法来处理的文件系统的路径。相对的方法是.is_file().is_dir()

In [1]: from pathlib import Path

In [2]: p = Path('/usr')

In [3]: p.is_file()
Out[3]: False

In [4]: p.is_dir()
Out[4]: True

In [5]: q = p / 'bin' / 'vim'

In [6]: q.is_file()
Out[6]: True

In [7]: q.is_dir()
Out[7]: False

也可以通过PyPi上的pathlib2模块在 Python 2.7 上使用Pathlib。

Python 3.4 introduced the pathlib module into the standard library, which provides an object oriented approach to handle filesystem paths. The relavant methods would be .is_file() and .is_dir():

In [1]: from pathlib import Path

In [2]: p = Path('/usr')

In [3]: p.is_file()
Out[3]: False

In [4]: p.is_dir()
Out[4]: True

In [5]: q = p / 'bin' / 'vim'

In [6]: q.is_file()
Out[6]: True

In [7]: q.is_dir()
Out[7]: False

Pathlib is also available on Python 2.7 via the pathlib2 module on PyPi.


回答 3

import os

if os.path.isdir(d):
    print "dir"
else:
    print "file"
import os

if os.path.isdir(d):
    print "dir"
else:
    print "file"

回答 4

os.path.isdir('string')
os.path.isfile('string')

os.path.isdir('string')
os.path.isfile('string')

回答 5

试试这个:

import os.path
if os.path.isdir("path/to/your/file"):
    print "it's a directory"
else:
    print "it's a file"

try this:

import os.path
if os.path.isdir("path/to/your/file"):
    print "it's a directory"
else:
    print "it's a file"

回答 6

如果您只是浏览一组目录,则最好尝试尝试os.chdir给出错误/警告(如果失败):

import os,sys
for DirName in sys.argv[1:]:
    SaveDir = os.getcwd()
    try:
        os.chdir(DirName)
        print "Changed to "+DirName
        # Do some stuff here in the directory
        os.chdir(SaveDir)
    except:
        sys.stderr.write("%s: WARNING: Cannot change to %s\n" % (sys.argv[0],DirName))

If you’re just stepping through a set of directories you might be better just to try os.chdir and give an error/warning if it fails:

import os,sys
for DirName in sys.argv[1:]:
    SaveDir = os.getcwd()
    try:
        os.chdir(DirName)
        print "Changed to "+DirName
        # Do some stuff here in the directory
        os.chdir(SaveDir)
    except:
        sys.stderr.write("%s: WARNING: Cannot change to %s\n" % (sys.argv[0],DirName))