Python文件、目录的一些操作(新增、移动、删除),,创建文件: os.mkn


创建文件:

os.mknod("test.txt") #创建孔文件 open("test.txt",w) #直接打开一个文件,如果文件不存在则创建文件```创建目录:```os.mkdir("file")```复制文件```shutil.copyfile("oldfile","newfile") #oldfile 和 newfile 都只能是文件 shutil.copy("oldfile","newfile") #oldfile 只能是文件夹,newfile 可以使文件,也可以是目录```复制文件夹```shutil.copytree("olddir","newdir") #oldfile 和 newfile都只能是目录,且newdir必须不存在```重命名文件或者目录:```os.rename("oldname","newname")```移动文件(目录):```shutil.move("oldpos","newpos")```删除文件:```os.remove("file")```删除目录:```os.rmdir("dir") #只能删除空目录 shutil.rmtree("dir") #空目录、有内容的目录都可以删除```转换目录:```os.chdir("path")```判断是目录还是文件:```os.path.exists("goal") #判断目标是否存在 os.path.isdir("goal") #判断目标是否是目录 os.path.isfile("goal") #判断目标是否是文件

评论关闭