import os # dir = os.getcwd() print(dir) # 查看777.py在那个文件 # all = os.listdir(r'D:\kinggsoft') # print(all) #查看文件里面有什么文件夹 # 创造文件夹 # r = os.mkdir(r'D:\kinggsoft\ppd') # print(r) # os.removedirs(r'D:\kinggsoft\ppd') # 删除ppd文件夹 # 切换目录,把现在777.py的目录切换到r'D:\kinggsoft',然后再查看当前目录就变成r'D:\kinggsoft'了 # f = os.chdir(r'D:\kinggsoft') # print(f) # path = os.getcwd() # print(path) # # path = os.path.abspath('aa.txt') # print(path) # 获得当前py文件的绝对路径D:2\pythonlesson767.py # path = os.path.abspath(__file__) # print(path) # # # # # 当前目录绝对路径: D:2\pythonlesson76 # path = os.getcwd() # print(path) # 分割获得777.py # path = r'D:2\pythonlesson767.py' # result = os.path.split(path) # print(result) # print(result[1]) # # # 分割文件和扩展名 # result = os.path.splitext(path) # print(result) # 添加后缀'file', 'a', 'a1.jpg' # result = os.path.join(os.getcwd(), 'file', 'a', 'a1.jpg') # print(result) # 删除ks文件夹 path = r'D:\kinggsoft\ppx\ks' filelist = os.listdir(path) for file in filelist: path1 = os.path.join(path, file) os.remove(path1) else: os.rmdir(path) print('删除成功') # 把一个文件里面的内容复制到另外一个文件里面去,但是如果一个文件里面还有一个文件,就不能复制过去会报错 # src_path = r'D:\kinggsoft\ppx' # target_path = r'D:\kinggsoft\ks' # def copy(src, target): # if os.path.isdir(src) and os.path.isdir(target): # filelist = os.listdir(src) # for file in filelist: # path = os.path.join(src, file) # with open(path, 'rb') as rstream: # container = rstream.read() # path1 = os.path.join(target,file) # with open(path1, 'wb') as wstream: # wstream.write(container) # else: # print('复制完毕') # copy(src_path, target_path) # # 把一个文件里面的内容复制到另外一个文件里面去,也能复制文件夹加里面的内容 # src_path = r'D:\kinggsoft\ppx' # target_path = r'D:\kinggsoft\ks' # def copy(src_path, target_path): # filelist = os.listdir(src_path) # for file in filelist: # path = os.path.join(src_path, file) # if os.path.isdir(path): # target_path1 = os.path.join(target_path, file) # 在复制过去的那边一个添加文件夹 # os.mkdir(target_path1) # 在复制过去的那边一个添加文件夹 这两行就可以 # copy(path, target_path) # else: # with open(path, 'rb') as rstream: # container = rstream.read() # path1 = os.path.join(target_path, file) # with open(path1, 'wb') as wstream: # wstream.write(container) # else: # print('复制完毕') # copy(src_path, target_path)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)