os.path.exists(pathname)
os.path.isfile(pathname)
os.path.isdir(pathname)
os.mknod(filename) #windows下不可用 open(filename, "w") #记得要关闭
shutil.copyfile("oldfile", "newfile") #oldfile和newfile都只能是文件
shutil.copy("oldfile", "newfile") #oldfile只能是文件,newfile可以是文件,也可以是目标目录
os.remove(filename)
file = open("test.txt", w)
file.seek(0)
file.truncate() #注意文件指针的位置
file.close()
os.mkdir(pathname) #创建单级目录 os.makedirs(pathname) #递归创建多级目录
shutil.copytree("olddir", "newdir")
#olddir和newdir都只能是目录,且newdir必须不存在
os.rename(oldname, newname)
shutil.move(oldpath, newpath)
os.rmdir("dir") #不能删除非空目录
'''
#可以删除非空目录,目录打开时也能删除
#约等于'rd /Q /S dir'
'''
shutil.rmtree("dir")
#encoding=utf-8
#适用于python3.5+
import os, sys, time, shutil
#清空目录
def ClearDir(dir):
print('ClearDir ' + dir + '...')
for entry in os.scandir(dir):
if entry.name.startswith('.'):
continue
if entry.is_file():
os.remove(entry.path) #删除文件
else:
shutil.rmtree(entry.path) #删除目录
os.chdir(newpath)
>>> pathfile = r'D:abcdefghi.txt' >>> os.path.dirname(pathfile) 'D:\abc\def' >>> os.path.basename(pathfile) 'ghi.txt'
os.path.getsize(pathfile) #单位为字节(Byte)
import os, sys
if __name__ == "__main__":
os.chdir('E:\')
print(sys.path[0])
print(os.path.abspath('.'))
print(os.path.dirname(os.path.abspath(__file__)))
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有