Case: Python implementation code file backup machine

Source: Internet
Author: User

Manage directories and files just want the file format I want
# 遍历 import rere_filename = re.compile(‘(.*pdf$)|(.*docx$)|(.*xlsx$)‘)  for root,dir,files in os.walk(‘.‘):   # 遍历 当前路径、目录、文件,接着下级路径、目录、文件    print(root,dir,files)
. [‘目录1‘, ‘目录2‘] [‘3.1-管理目录与文件w.pdf‘, ‘3.2-处理任意格式的文本文件w.pdf‘, ‘3.3-python操作办公文件w.pdf‘, ‘a.docx‘, ‘b.xlsx‘, ‘绝密文件.pdf‘].\目录1 [] [‘1.txt‘].\目录2 [] [‘2.txt‘]
# 显示根目录内所有文件import refor root,dir,files in os.walk(‘.‘):    for name in files:        file = os.path.join(root,name)  # 根目录的文件        print(file)
.\3.1-管理目录与文件w.pdf.\3.2-处理任意格式的文本文件w.pdf.\3.3-python操作办公文件w.pdf.\a.docx.\b.xlsx.\绝密文件.pdf.\目录1\1.txt.\目录2\2.txt
# 显示根目录内.pdf、.docx、.xlsx文件import rere_filename = re.compile(‘(.*pdf$)|(.*docx$)|(.*xlsx$)‘)for root,dir,files in os.walk(‘.‘):    for name in files:        file = os.path.join(root,name)        if re_filename.match(file):            print(file)
.\3.1-管理目录与文件w.pdf.\3.2-处理任意格式的文本文件w.pdf.\3.3-python操作办公文件w.pdf.\a.docx.\b.xlsx.\绝密文件.pdf
Compress backup multiple Files
# 创建压缩文件import zipfile  my_zip = zipfile.ZipFile(‘busy.zip‘,‘w‘)  # 以只写的方式打开压缩文件,并创建对象my_zip.write(‘a.docx‘,compress_type=zipfile.ZIP_DEFLATED)my_zip.write(‘b.xlsx‘,compress_type=zipfile.ZIP_DEFLATED)my_zip.close()os.listdir()
[‘3.1-管理目录与文件w.pdf‘, ‘3.2-处理任意格式的文本文件w.pdf‘, ‘3.3-python操作办公文件w.pdf‘, ‘a.docx‘, ‘b.xlsx‘, ‘busy.zip‘, ‘目录1‘, ‘目录2‘, ‘绝密文件.pdf‘]
# 查看压缩文件see = zipfile.ZipFile(‘busy.zip‘,‘r‘)  # 以只读的方式打开文件,并创建对象see.namelist()
[‘a.docx‘, ‘b.xlsx‘]
see.getinfo(‘a.docx‘)
<ZipInfo filename=‘a.docx‘ compress_type=deflate filemode=‘-rw-rw-rw-‘ file_size=0 compress_size=2>
see.close()
# 复制压缩文件import shutilshutil.copy(‘busy.zip‘,‘busy-1.zip‘)
‘busy-1.zip‘
os.listdir()
[‘3.1-管理目录与文件w.pdf‘, ‘3.2-处理任意格式的文本文件w.pdf‘, ‘3.3-python操作办公文件w.pdf‘, ‘a.docx‘, ‘b.xlsx‘, ‘busy-1.zip‘, ‘busy.zip‘, ‘目录1‘, ‘目录2‘, ‘绝密文件.pdf‘]
# 解压压缩文件busy = zipfile.ZipFile(‘busy-1.zip‘)  # 创建对象busy.extractall(‘目录1‘)  # 解压到指定目录busy.close()
os.chdir(‘目录1‘)os.listdir()
[‘1.txt‘, ‘a.docx‘, ‘b.xlsx‘]
Case: Code Backup machine
def auto_name():    passdef zip_all():    passdef zip_by_name():    pass
import zipfileimport osdef zip_all(from_dir,target_zip):    """把目录中的每个文件写入zip文件"""    my_zip = zipfile.ZipFile(target_zip,‘w‘)    for root,dir,files in os.walk(from_dir):   # 遍历目录内的所有目录和文件,返回列表        for file in files:                     # 遍历文件            filename = os.path.join(root,file) # 路径+文件名            print(filename)            my_zip.write(filename,compress_type=zipfile.ZIP_DEFLATED) # 写入文件    my_zip.close()     # 关闭def auto_name(source_name):    if source_name:        new_name = source_name.split(‘-‘)[0] + ‘-‘ + str(int(source_name.split(‘-‘)[1].split(‘.‘)[0])+1) + ‘.zip‘    return new_namebase_dir = r‘E:\迅雷下载‘  # 路径target_zip = os.path.join(base_dir,auto_name(‘a-1.zip‘))  # 需要新建的压缩文件名from_dir = os.path.join(base_dir,‘test‘)                  # 需要遍历的目录zip_all(from_dir,target_zip)
E:\迅雷下载\test\a-1.txtE:\迅雷下载\test\a-11.txtE:\迅雷下载\test\a-2.txtE:\迅雷下载\test\a-3.txt

Case: Python implementation code file backup machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.