用Python关于 文件的复制移动压缩解压

  ```python

  import os

  import glob

  import time

  import datetime

  # from tempfile import TemporaryFile

  #

  # with TemporaryFile('w+') as f:

  # f.write('hello')

  # f.seek(0)

  # date = f.readlines()

  # print(date)

  # os.mkdir('新建文件夹')

  #批量创建十个txt文件

  # for i in range(10):

  # f=open("%s"%i+'.txt','a')

  # #./ 代表是当前的文件夹,%i表示文件的名称,a表示没有改文件夹就新建

  #

  # f.write("")

  # f.close()

  # 批量创建5 个文件夹

  # import os

  # #设置文件夹的位置

  # path = '.dar1/'

  # for i in range (5):

  # #定义一个变量判断文件是否存在。

  # isExists = os.path.exists(path+str(i))

  # if not isExists:

  # os.makedirs(path+str(i))

  # print("%s目录创建成功"%i)

  # else:

  # print("%s目录已经存在"%i)

  # continue

  # 复制文件和文件夹

  # import shutil

  # # shutil.copy(要复制的文件,要复制到的文件)

  # # 文件夹

  # # 路径复制并重命名

  # shutil.copy('file2.txt','./a1') #复制

  # shutil.copy('file2.txt','./a1/file3.txt')#复制并重命名

  # 复制文件夹shutil.copytree()

  # shutil.copytree('a1','./a1/a2')

  # # 移动 重名 和删除

  # import shutil

  # shutil.move("2.txt","./a1")#移动文件

  # os.rename("1.txt","0.txt") #重命名文件和文件夹

  # os.remove('0.txt') #删除文件

  # shutil.rmtree("a2")#删除目录

  #读取压缩包内文件

  #zipfile --后缀为zip的文件

  # import zipfile

  # with zipfile.ZipFile("a0.zip","r") as zipobj:

  # for filename in zipobj.namelist():

  # print(filename)

  #如何解决压缩名为中文的文件会出现乱码的问题

  # import zipfile枣庄人流医院排行榜 http://mobile.3635555.cn/

  # with zipfile.ZipFile("学习文件.zip","r") as zipobj:

  # for filename in zipobj.namelist():

  # print(filename.encode("cp437").decode("gbk"))

  # #创建zip压缩包

  # import zipfile

  # with zipfile.ZipFile("x1.zip","w") as zipobj:

  # zipobj.write("0.txt")

  # # 批量文件创建压缩包

  # file_name = ["0.txt","1.txt","2.txt","3.txt"]

  # with zipfile.ZipFile("x1.zip","w") as zipobj:

  # for filen in file_name:

  # zipobj.write(filen)

  # # 读取压缩包文件

  # with zipfile.ZipFile("x1.zip","r") as zipobj:

  # print(zipobj.namelist())

  #

  # file_name = ["4.txt","5.txt","6.txt","7.txt"]

  # with zipfile.ZipFile("x1.zip","a") as zipobj:

  # for filen in file_name:

  # zipobj.write(filen)

  # # 读取压缩包文件

  # with zipfile.ZipFile("x1.zip","r") as zipobj:

  # print(zipobj.namelist())

  #解压文件

  # import zipfile

  # with zipfile.ZipFile("a0.zip","r") as zipobj :

  # zipobj.extractall("./a2")

  # # zipobj.extractall("./a2",pwd='123456')


猜你喜欢

转载自blog.51cto.com/14503791/2645459

相关文章