# _*_ coding:utf-8 _*_# @Time :2020/1/8 23:33# @Author :dery# @File :file_operate.py# @Software :PyCharm'''源文件: c:\p1\girl.jpg目标文件:c:\p2\girl.jpg'''import os# 文件的复制# with open(r'D:\pythoncode\p1\girl.jpg', 'rb') as stream:# container = stream.read()# print(stream.name)# file = stream.name# filename = file[file.rfind('\\')+1:]# print(filename)# with open(r'D:\pythoncode\p2\girl.jpg', 'wb') as wstream:# wstream.write(container)# print('文件复制')# 获取路径# path = os.path.dirname(__file__) # 获取当前文件所在的文件目录(绝对路径)# print(path)# result = os.path.join(path, 'a1.jpg') # 返回的是一个拼接后的新的路径# r = os.path.isabs(r'D:\pythoncode\p1\girl.jpg')# print(r)# r = os.path.isabs(r'../girl.jpg')# print(r)# 通过相对路径获取绝对路径# path = os.path.abspath('girl.jpg')# print(path)# 获取当前文件的绝对路径# path = os.path.abspath(__file__)# print(path)# 获取当前文件所在目录的绝对路径# path = os.getcwd() # 类似os.path.dirname(__file__)# print(path)# 获取文件名称# result = os.path.split(path)# print(result[1])# 获取文件扩展名# result = os.path.splitext(path)# print(result)# 获取文件大小,单位是字节# result = os.path.getsize(path)# print(result)# 返回指定目录下所有的文件及文件夹,保存在列表中# all = os.listdir(r'D:\pythoncode\p1')# print(all)# 创建文件夹# if not os.path.exists(r'D:\pythoncode\p3'):# f = os.mkdir(r'D:\pythoncode\p3')# print(f)# 删除文件夹,只能删除空文件夹# f = os.rmdir(r'D:\pythoncode\p3')# print(f)# path = r'D:\pythoncode\p3'# file_list = os.listdir(path)# for file in file_list:# path1 = os.path.join(path, file)# os.remove(path1)# else:# os.rmdir(path)# print('删除成功')# 切换目录f = os.chdir(r'D:\pythoncode\p2')print(f)path = os.getcwd()print(path)
来源:https://www.cnblogs.com/python-beginner/p/12174343.html