Python3-os模块

 1 import os
 2  
 3 with open(r"F:\FileOperation\p2\demoDaoJi.jpg",'rb') as stream:
 4     container = stream.read()
 5     path = os.path.dirname(__file__) 
 6     # path获取当前文件所在的文件目录(绝对路径)
 7     path1 = os.path.join(path,'demoDaoJi.jpg') 
 8     # path1返回的是一个拼接后的新的路径
 9     with open(path1,'wb') as wstream:
10     wstream.write(container)
11  
12 print("复制完成!")
13  
14 """
15 file = stream.name # 获取文件名(绝对路径)
16 filename = file[file.rfind(r"\\")+1:] # 截取文件名
17 path = os.path.dirname(__file__) # 获取当前文件所在的文件目录(绝对路径)
18 path1 = os.path.join(path,filename) #
19 """
 
os.path.isabs(路径)    ---> 判断是否为绝对路径
os.path.abspath(文件名)      ---> 通过相对路径获取绝对路径
os.path.abspath(__file__)    ---> 获取当前文件的绝对路径
os.getcwd()            ---> 类似os.path.abspath(__file__),获取当前文件所在的文件夹
os.path.split(路径)    ---> 分割(目录、文件名)
os.path.splitext(路径)    ---> 分割(文件、扩展名)
os.path.getsize(路径)     ---> 获取文件大小    
os.listdir(路径)    ---> 返回指定路径下的所有文件及文件夹,保存至列表里
os.mkdir(路径)    ---> 创建文件夹
os.rmdir()        ---> 删除指定文件夹(必须是空文件夹)
os.removedirs()    ---> 删除多个目录
os.remove()        ---> 删除指定文件
os.path.exist()    ---> 判断文件是否存在
os.chdir()        ---> 切换目录
 
 
示例1:复制文件里的所有文件(一层)
 1 import os
 2  
 3 src = r"F:\FileOperation\p1"
 4 target = r"F:\FileOperation\p2"
 5  
 6 def copy(src,target):
 7     if os.path.isdir(src) and os.path.isdir(target): 
 8     # 判断src和target是否为文件夹
 9     pathlist = os.listdir(src) # 获取文件目录
10     for file in pathlist:
11         path = os.path.join(src,file) # 拼接获取文件的绝对路径
12         with open(path,'rb') as stream:
13             container = stream.read()
14             path1 = os.path.join(target,file)
15             with open(path1,'wb') as wstream:
16                 wstream.write(container)
17     else:
18         print("复制完毕!")
19  
20 copy(src,target)

 

示例2:复制文件里的所有文件(多层)
 1 """
 2 设计思路:
 3 1.判断src和target是否为文件夹
 4 2.如果是,对src进行os.listdir()获取目录
 5 3.遍历src内层,再进行判断os.path.isdir(),如果是文件夹,再进行调用copy(),同时在target创建同名文件夹
 6 4.创建文件夹时候,注意不要重名,否则会将target原路径覆盖
 7 """
 8  
 9 import os
10  
11 src = r"F:\FileOperation\pp1"
12 target = r"F:\FileOperation\pp2"
13  
14 def copy(src,target):
15     if os.path.isdir(src) and os.path.isdir(target):
16         filelist = os.listdir(src)
17         for file in filelist:
18             path = os.path.join(src, file)
19             if os.path.isdir(path):
20                 target1 = os.path.join(target,os.path.split(path)[1])
21                 os.mkdir(target1) # 在pp2中创建p1文件夹
22                 copy(path,target1)
23             else:
24                 with open(path,'rb') as stream:
25                     container = stream.read()
26                     path1 = os.path.join(target,file)
27                     with open(path1,'wb') as wstream:
28                         wstream.write(container)
29        else:
30             print("复制完毕!")
31  
32 copy(src,target)
33  
34 """
35 os.path.split(path) ----> ('F:\\FileOperation\\pp1', 'p1')
36 os.path.split(path)[1] ---> 'p1'
37 os.path.join(target,os.path.split(path)[1]) ---> 得到路径 F:\\FileOperation\\pp2\\p1
38 """

 

posted @ 2020-05-31 13:20  闹点小情绪q  阅读(117)  评论(0编辑  收藏  举报