简体   繁体   English

是否有用于临时备份和还原Python中文本文件的模块

[英]Are there a modules for temporarily backup and restore text files in Python

I need to modify a text file at runtime but restore its original state later (even if the computer crash). 我需要在运行时修改文本文件,但稍后再恢复其原始状态(即使计算机崩溃)。

My program runs in regular sessions. 我的程序定期运行。 Once a session ended, the original state of that file can be changed, but the original state won't change at runtime. 会话结束后,可以更改该文件的原始状态,但在运行时不会更改原始状态。

There are several instances of this text file with the same name in several directories. 该文本文件的多个实例在多个目录中具有相同的名称。 My program runs in each directory (but not in parallel), but depending on the directory content's it does different things. 我的程序在每个目录中运行(但不是并行运行),但是根据目录内容的不同,它会执行不同的操作。 The order of choosing a working directory like this is completely arbitrary. 这样选择工作目录的顺序完全是任意的。

Since the file's name is the same in each directory, it seems a good idea to store the backed up file in slightly different places (ie. the parent directory name could be appended to the backup target path). 由于每个目录中文件的名称都相同,因此将备份文件存储在稍有不同的位置(例如,父目录名称可以附加到备份目标路径)似乎是个好主意。

What I do now is backup and restore the file with a self-written class, and also check at startup if the previous backup for the current directory was properly restored. 我现在要做的是使用自写类备份和还原文件,并在启动时检查当前目录的先前备份是否已正确还原。 But my implementation needs serious refactoring, and now I'm interested if there are libraries already implemented for this kind of task. 但是我的实现需要认真的重构,现在我很想知道是否已经为此类任务实现了库。

edit 编辑

version control seems like a good idea, but actually it's a bit overkill since it requires network connection and often a server. 版本控制似乎是一个好主意,但实际上有点过头了,因为它需要网络连接并且通常需要服务器。 Other VCS need clients to be installed. 其他VCS需要安装客户端。 I would be happier with a pure-python solution, but at least it should be cross-platform, portable and small enough (<10mb for example). 我会更喜欢纯Python解决方案,但至少它应该是跨平台的,可移植的并且足够小(例如,<10mb)。

Implement Version control ... like svn (see pysvn) it should be fast as long as the repo is on the same server... and allows rollbacks to any version of the file... maybe overkill but that will make everything reversible 实施版本控制...像svn(请参见pysvn)一样,只要存储库位于同一服务器上就应该很快...并且允许回滚到该文件的任何版本...也许过大了,但这将使所有内容都可逆

http://pysvn.tigris.org/docs/pysvn_prog_guide.html http://pysvn.tigris.org/docs/pysvn_prog_guide.html

You dont need a server ... you can have local version control and it should be fine... 您不需要服务器...您可以具有本地版本控制,应该没问题...

Why not just do what every unix , mac , window file has done for years -- create a lockfile/working file concept. 为什么不做每一个unix,mac,window文件多年来所做的事情-创建一个lockfile / working file概念。

When a file is selected for edit: 选择文件进行编辑时:

  1. Check to see if there is an active lock or a crashed backup. 检查是否有活动的锁或崩溃的备份。
  2. If the file is locked or crashed, give a "recover" option 如果文件被锁定或崩溃,请提供“恢复”选项
  3. Otherwise, begin editing the file... 否则,开始编辑文件...

The editing tends to do one or more of a few things: 编辑倾向于执行以下一项或多项操作:

  1. Copy the original file into a ".%(filename)s.backup" 将原始文件复制到“。%(filename)s.backup”中
  2. Create a ".%(filename)s.lock" to prevent others from working on it 创建一个“。%(filename)s.lock”以防止其他人对其进行操作
  3. When editing is achieved, the lock goes away and the .backup is removed 完成编辑后,锁将消失,.backup将被删除。

Sometimes things are slightly reversed, and the original stays in place while a .backup is the active edit; 有时情况略有相反,当.backup是活动编辑时,原始文件保留在原位; on success the .backup replaces the original 成功的取代的.backup原

If you crash vi or some other text programs on a linux box, you'll see these files created . 如果在Linux机器上使vi或某些其他文本程序崩溃,则会看到这些文件已创建。 note that they usually have a dot(.) prefix so they're normally hidden on the command line. 需要注意的是,他们通常有一个点(。)前缀,所以他们通常隐藏在命令行上。 Word/Powerpoint/etc all do similar things. Word / Powerpoint / etc都做类似的事情。

Git,Subversion或Mercurial是您的朋友。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM