文章来自《Python cookbook》.

翻译仅仅是为了个人学习,其它商业版权纠纷与此无关!

-- 218.25.65.60 [2004-09-30 19:40:28]

1. 描述

  • Versioning Filenames

处理文件版本

Credit: Robin Parmar

1.1. 问题 Problem

You want make a backup copy of a file, before you overwrite it, with the standard protocol of appending a three-digit version number to the name of the old file.

在更新文件前,需要进行备份 : 按照标准协议是在原文件末尾加3位数字作为版本号来备份原文件

1.2. 解决 Solution

This simple approach to file versioning uses a function, rather than wrapping file objects into a class:

文件版本处理的简单方法是使用一个函数, 不需要用类包装文件对象 :

   1 
   2 def VersionFile(file_spec, vtype='copy'):
   3     import os, shutil
   4 
   5     if os.path.isfile(file_spec):
   6         # or, do other error checking:
   7         if vtype not in 'copy', 'rename':
   8              vtype = 'copy'
   9 
  10         # Determine root filename so the extension doesn't get longer
  11         n, e = os.path.splitext(file_spec)
  12 
  13         # Is e an integer?
  14         try:
  15              num = int(e)
  16              root = n
  17         except ValueError:
  18              root = file_spec
  19 
  20         # Find next available file version
  21         for i in xrange(1000):
  22              new_file = '%s.%03d' % (root, i)
  23              if not os.path.isfile(new_file):
  24                   if vtype == 'copy':
  25                       shutil.copy(file_spec, new_file)
  26                   else:
  27                       os.rename(file_spec, new_file)
  28                   return 1
  29 
  30     return 0
  31 
  32 if _ _name_ _ == '_ _main_ _':
  33       # test code (you will need a file named test.txt)
  34       print VersionFile('test.txt')
  35       print VersionFile('test.txt')
  36       print VersionFile('test.txt')

1.3. 讨论 Discussion

The purpose of the VersionFile function is to ensure that an existing file is copied (or renamed, as indicated by the optional second parameter) before you open it for writing or updating and therefore modify it. It is polite to make such backups of files before you mangle them. The actual copy or renaming is performed by shutil.copy and os.rename, respectively, so the only issue is what name to use as the target.

上面VersionFile函数的目的是在打开文件写入或更新从而修改文件前备份现有文件(或者如参数指定的进行重命名操作)。在文件混乱前备份文件是不错的。 实际的copy和rename操作分别是由shutil.copy和os.rename函数完成的,唯一问题是使用什么作为新的目标文件名称。

A popular way to determine backups' names is versioning (i.e., appending to the filename a gradually incrementing number). This recipe determines the new_name by first extracting the filename's root (just in case you call it with an already-versioned filename) and then successively appending to that root the further extensions .000, .001, and so on, until a name built in this manner does not correspond to any existing file. Then, and only then, is the name used as the target of a copy or renaming. Note that VersionFile is limited to 1,000 versions, so you should have an archive plan after that. You also need the file to exist before it is first versioned梱ou cannot back up what does not yet exist.

通用的确定备份文件名称得方法是使用版本号(在文件名称末尾附加逐渐增加的数字)。本食谱方法首先分解出文件名称的根部分(防止在已经具有版本数字的文件名称后面添加数字),然后连续添加如 .000, .001这样的名称扩展以获得新的文件名称,直到获得的文件名称未作为一个文件存在,就得到了备份或者重命名的文件名。 注意,VersionFile仅能处理到1000的版本,在1000以后,应该有一个对文档进行归档处理的计划。 备份或重命名前文件必须存在,不能对一个不存在的文件进行备份。

This is a lightweight implementation of file versioning. For a richer, heavier, and more complete one, see Recipe 4.27.

这是文件版本处理的一个轻量实现,功能更强,更复杂,更完整的方法实现见食谱4.27.

1.4. 参考 See Also

Recipe 4.27;

食谱4.27

documentation for the os and shutil modules in the Library Reference.

Python文档os模块、shutil模块部分