• 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('')
  • 相关阅读:
    LeetCode 242. Valid Anagram (验证变位词)
    LeetCode 205. Isomorphic Strings (同构字符串)
    LeetCode 204. Count Primes (质数的个数)
    LeetCode 202. Happy Number (快乐数字)
    LeetCode 170. Two Sum III
    LeetCode 136. Single Number (落单的数)
    LeetCode 697. Degree of an Array (数组的度)
    LeetCode 695. Max Area of Island (岛的最大区域)
    Spark中的键值对操作
    各种排序算法总结
  • 原文地址:https://www.cnblogs.com/vito13/p/7737148.html
Copyright © 2020-2023  润新知