python怎么判断文件是否存在

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

⑵ 我如何检查文件是否存在使用Python

使用 os 模块。

importos

dir="你的文件来或者文件夹目录自"

#一种常用的判断文件目录不存在并创建目录的方法
#当然如果只是判断是否存在,同样适用于文件
ifnotos.path.exists(dis_dir):
os.mkdir(dis_dir)

⑶ Python3如何检查文件是否存在

importos
deffilecheck(path):
ifos.path.exists(path):
print("%sisexist"%path)

⑷ python中如何判断目录内是文件还是文件夹

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,文件夹名和同级目录的文件名是不可以同时存在的。
zhangpeng@Earth:~$ mkdir te
mkdir: cannot create directory `te': File exists
zhangpeng@Earth:~$ rm te
zhangpeng@Earth:~$ mkdir te
zhangpeng@Earth:~$ > te
-bash: te: Is a directory

⑸ python如何用if判断文件夹是否存在

python用复if判断文件夹制是否存在的方法:

python的os模块可以对文件夹进行操作。使用if语句“os.path.exists()”函数的返回值是否是True,如果是则输出该文件夹存在

示例:判断文件kk是否存在

代码如下:

执行结果如下:

更多Python知识,请关注:Python自学网!!

⑹ python判断固定文件夹下某个固定后缀的文件是否存在

#import os

pathlist = os.listdir('/opt/text')

for filename in pathlist:

if filename.endswith('sw'):

print filename

⑺ python 文件夹下是否有文件路径

importos
#判断文件夹路径
ifnotos.path.exists('thepath'):
#存在该方法专返回True
os.makedirs('thepath')
#判断文件是否属存在
ifos.path.isfile('thepath')
#文件存在

⑻ python 如何判断某一目录下以abc开头的文件是否存在

正好看到这里了

python有专门完成这个任务的模块glob


importglob
files=glob.glob('test/abc*.txt')

⑼ python如何判断一个目录下是否存在某个文件谢啦!

使用os.path.exists()方法可以直接判断文件是否存在。
代码如下:
>>> import os
>>> os.path.exists(r'C:\1.TXT')
False
>>>
如果存在返回值为True如果不存在则返回False。很方便
希望对你有所帮助~~