zoukankan      html  css  js  c++  java
  • Python3-笔记-F-001-组织文件

    import shutil, os, zipfile

    # 复制、移动、改名、删除文件与文件夹
    #shutil.copy('e:\111\key.txt', 'd:\111\') # copy文件
    #shutil.copytree('e:\111', 'd:\111\aaa') # copy目录。且目标文件夹需不存在
    #shutil.move('e:\111\key.txt', 'e:\111\key2.txt') # 改文件名
    #shutil.move('e:\111', 'e:\112') # 改文件夹名
    #shutil.move('e:\112\key2.txt', 'd:\111\key22.txt') # 移动文件
    #shutil.move('e:\112', 'd:\111') # 移动文件夹
    #shutil.rmtree('d:\111\112') # 不可恢复地删除文件夹
    #os.unlink('d:\111\key22.txt') # 删除文件

    '''#删除文件或文件夹到回收站
    import send2trash
    send2trash.send2trash('d:\111')
    baconFile = open('bacon.txt', 'a') # creates the file
    baconFile.write('Bacon is not a vegetable.')
    baconFile.close()
    send2trash.send2trash('bacon.txt')


    # 遍历目录树,压缩到zip
    newZip = zipfile.ZipFile('c:\new.zip', 'w')
    for folderName, subfolders, filenames in os.walk('D:\xiaomi'):
    print('The current folder is ' + folderName)
    for subfolder in subfolders:
    print('SUBFOLDER OF ' + folderName + ': ' + subfolder)
    newZip.write(folderName+'\'+subfolder, compress_type=zipfile.ZIP_DEFLATED)
    for filename in filenames:
    print('FILE INSIDE ' + folderName + ': '+ filename)
    newZip.write(folderName+'\'+filename, compress_type=zipfile.ZIP_DEFLATED)
    print('')
    newZip.close()

    '''
    # 解压缩zip到指定文件夹
    exampleZip = zipfile.ZipFile('c:\new.zip')
    for file_name in exampleZip.namelist():
    print('File:', file_name, end = ' ')
    file_bytes = exampleZip.read(file_name)
    print('has ', len(file_bytes), ' bytes', end = ' ')
    spamInfo = exampleZip.getinfo(file_name)
    if spamInfo.file_size and spamInfo.compress_size:
    print('Compressed file is %sx smaller!' % (round(spamInfo.file_size / spamInfo.compress_size, 2)))
    print()
    # exampleZip.extractall("d:\333")
    # exampleZip.extract('xiaomi/MiPhoneManager/LocalCache/6O5513A60360/' , 'd:\333\') # 可以解压一个文件或一个空文件夹名称
    exampleZip.close()

    print('')
  • 相关阅读:
    mac 界面以及界面浏览器等显示不全
    数组常用的方法总结
    工厂模式以及应用场景
    结构与布局-紧贴底部的页脚
    结构与布局-垂直居中
    vscode mac下终端code .快速打开工程文件
    javascript操作对象和集合(jquery 6)
    2018 Multi-University Training Contest 8
    2018 Multi-University Training Contest 8
    BZOJ 3196 二逼平衡树
  • 原文地址:https://www.cnblogs.com/vito13/p/7737148.html
Copyright © 2011-2022 走看看