python 判断文件名是否有指定字符串

Python013

python 判断文件名是否有指定字符串,第1张

a = 'abc' #--------------------要查询的字符串

with open('1.txt','r') as foo:

for line in foo.readlines():

if a in line:

print line

look~~

>>> os.path.exists("te")

True

>>> os.path.exists("nothing")

False

>>> os.path.isfile("nothing")

False

>>> os.path.isdir("nothing")

False

>>>

>>> os.path.isdir("te")

False

>>> os.path.isfile("te")

True

>>>

建议你先判断是否存在,如果确实存在,你再进行判断是文件还是文件夹

-------------------------

Linux,文件夹名和同级目录的文件名是不可以同时存在的。

zhangzhipeng@Earth:~$ mkdir te

mkdir: cannot create directory `te': File exists

zhangzhipeng@Earth:~$ rm te

zhangzhipeng@Earth:~$ mkdir te

zhangzhipeng@Earth:~$ > te

-bash: te: Is a directory

--------------------------

windows中:

可以证明,不管windows还是linux,同级目录下,是不允许出现同名文件(夹)的

但是这不能代表不是文件就是文件夹啊,首先得确认这个文件(夹)是存在的。

其实,文件夹也是一个文件。

遍历文件夹下找到所有文件

判断当前目录下文件的文件名是否是纯数字的,然后加入到列表中输出。

import os

num_filelist = []

for root,dirs,files in os.walk(r"E:\test"):

    for tmp in files:

        (shotname,extension) = os.path.splitext(tmp)

        if shotname.isalnum():

            num_filelist.append(os.path.join(root,tmp))

            

print num_filelist