Table of Contents
1 判断文件是否存在
os.path.exists()
当文件或文件夹存在时返回true。
os.path.is(file)
是文件的时候返回true,是文件夹时候返回false。
>>> import os
>>> link = r'C:\test'
>>> os.path.exists(link)
True
>>> os.path.isfile(link)
False
>>> a=os.path.join(link, 'a.txt')
>>> a
'C:\\test\\a.txt'
>>> os.path.isfile(a)
True
>>>
2 写入文件
# 方法一。不需要手动close文件
with open("test.txt","w") as file:
file.write("hello")
# 方法二。必须手动使用file.clo