【问题标题】:Creating a .zip archive with Python in MS DOS在 MS DOS 中使用 Python 创建 .zip 存档
【发布时间】:2010-11-24 20:07:02
【问题描述】:

我是编程新手。我正在尝试使用 CH Swaroop 的“Byte of Python”来学习 Python。一个例子是创建一个程序,它将一些文件从一个目录备份到另一个目录并将它们压缩成 .zip 格式。不幸的是,他给出的示例仅在您碰巧是 linux/unix 用户时才有用。对于 Windows 用户,他只说“Windows 用户可以使用 Info-Zip 程序”,但没有进一步详细说明。这是他提供的代码...

#!/usr/bin/python
# Filename : backup_ver1.py

import os
import time


# 1. The files and directories to be backed up are specified in a list.
source = [r'C:\Users\ClickityCluck\Documents']

# 2. The backup must be stored in a main backup directory
target_dir = r'C:\Backup'
# 3. Zip seems good

# 4. Let's make the name of the file the current date/time
target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.zip'

# 5. We use the zip command to put the files in a zip archive
zip_command = "zip -qr '%s' %s" % (target, ''.join(source))

# Run Backup
if os.system(zip_command) == 0:
    print "Succesful backup to", target
else:
    print 'BACKUP FAILED :('

任何人都可以在 Windows 7 的命令行中为我设计一种方法吗?感谢您的宝贵时间,如果我未能提供一些相关信息,我提前道歉:)

【问题讨论】:

  • 多么愚蠢的练习。 Python有zipfile,他求助于调用外部程序?最重要的是,它正在教授os.system(),这几乎已经过时了。浪费时间。
  • 但如果你不管...info-zip.org
  • 自 2000 年以来,Python 使用 zipfile 模块已有 。“Python 字节”比这要新得多。 docs.python.org/library/zipfile.html
  • 如果你想在命令行上做一些事情,你需要查找你正在使用的 zip 程序的文档。它将解释如何做到这一点。如果你真的想压缩文件,你应该使用 python zip 模块。
  • MS DOS?我猜你的意思是 Windows。

标签: python zip


【解决方案1】:

对于 python 3 用户并使用格式方法的答案应该是:

   # 1. The files and directories to be backed up are specified in a list.
   source = [r'C\Users\MySourceDir']

   # 2. The backup must be stored in a # main backup directory
   target_dir = r'C:\Users\MyTargetDir'

   # 3. The files are backed up into a zip file.
   # 4. The name of the zip archive is the current date and time
   target = target_dir + os.sep + time.strftime('%Y%m%d%H%M%S') + '.7z'

   # Create target directory if it is not present
   if not os.path.exists(target_dir):
       os.mkdir(target_dir)

   # 5. We use the zip command to put the files in a zip archive
   zip_command = 'C:\\"Program Files"\\7-Zip\\7z a -t7z -r "{0}" "{1}"'.format(target,' '.join(source)) # be careful with spaces on each dir specification. Problems could arise if double-quotation marks aren't between them. 

   # Run the backup 
   print('Zip command is:') 
   print(zip_command)
   print('Running:')

   if os.system(zip_command) == 0: 
       print('Successful backup to', target)
   else: 
       print('Backup FAILED')

【讨论】:

    【解决方案2】:

    zip 是用于创建/更新/提取 ZIP 存档的命令行实用程序,可在 Unix/Linux/Mac OS X 上使用。如果您想使用命令行实用程序存档文件,您应该在 (例如,compress 是资源包的一部分)。

    另一种方法是使用 python 的 zipfile 模块并为 windows 制作一个有用的命令行实用程序:)

    顺便说一句,为什么你的问题是指 MS DOS?

    【讨论】:

    • 为什么使用zipfile 是另一种方式呢?这应该是第一种方式。
    • 不是反对票,但是 - 考虑到 Python 从 2000 年开始就支持 zip 模块,他不应该调用系统命令在 Python 中制作 .zip 文件。
    • 有些人认为Windows命令提示符和MS-DOS是一样的。
    • @birryree @sukhbir 也许这是挑战的一部分?
    • zip 也可用于 Windows。但这不是重点。关键是这个练习做错了不止一件事。
    【解决方案3】:
    #!/usr/bin/python -tt
    # Filename: backup_ver1.py
    
    import os
    import time
    
    # 1. The files and directories to be backed up are specified in a list.
    source = ['C:\\Documents\\working_projects\\', 'C:\\Documents\\hot_tips\\']
    
    # 2. The back up must be stored in a main back directory
    target_dir = 'C:\\temp\\backup\\'
    
    # 3. The files are backed up into a zip file
    
    # 4. The name of the zip archive is the current date and time
    target = target_dir + time.strftime('%Y%m%d%H%M%S') + '.7z'
    
    # 5.  We use the zip command to put the files in a zip archive
    #zip_command = "zip -qr '%s' %s" % (target, ' '.join(source))
    zip_command = 'C:\\"Program Files"\\7-Zip\\7z a -t7z "%s" %s' % (target, ' '.join(source))
    
    # Run the backup
    if os.system(zip_command) == 0:
        print 'Successful back up to', target
    else:
        print 'Backup FAILED'
    

    【讨论】:

    • 确保在 windows 上使用 "\\" 而不是 "\": 'C:\\Documents\\hot_tips\\';上面的渲染删除了我所有的双反斜杠!
    • 上述代码的重点不是炫耀 Python 自己可以做什么,而是更多地展示它可以利用底层操作系统和内置的原始功能。
    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多