python 更新文本文件内容

Python039

python 更新文本文件内容,第1张

请详细阅读文档,使用a+的选项打开。

open(filename[, mode[, bufsize]])¶

Open a file, returning an object of the file type described in section File Objects. If the file cannot be opened, IOError is raised. When opening a file, it’s preferable to use open() instead of invoking the file constructor directly.

The first two arguments are the same as for stdio‘s fopen(): filename is the file name to be opened, and mode is a string indicating how the file is to be opened.

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.

The optional bufsize argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size. A negative bufsize means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used. [2]

Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text fileson systems that don’t have this distinction, adding the 'b' has no effect.

In addition to the standard fopen() values mode may be 'U' or 'rU'. Python is usually built with universal newline supportsupplying 'U' opens the file as a text file, but lines may be terminated by any of the following: the Unix end-of-line convention '\n', the Macintosh convention '\r', or the Windows convention '\r\n'. All of these external representations are seen as '\n' by the Python program. If Python is built without universal newline support a mode with 'U' is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple containing all the newline types seen.

Python enforces that the mode, after stripping 'U', begins with 'r', 'w' or 'a'.

Python provides many file handling modules including fileinput, os, os.path, tempfile, and shutil.

Changed in version 2.5: Restriction on first letter of mode string introduced.

ord(c)¶

python怎么监测svn库的文件有更新

1、将文件checkout到本地目录

svn checkout path(path是服务器上的目录)例如:svn checkout svn://192.168.1.1/pro/domain简写:svn co

登录后复制

2、往版本库中添加新的文件

svn add file例如:svn add test.php(添加test.php)svn add *.php(添加当前目录下所有的php文件)

登录后复制

3、将改动的文件提交到版本库

svn commit -m “LogMessage“ [-N] [--no-unlock] PATH(如果选择了保持锁,就使用–no-unlock开关)例如:svn commit -m “add test file for my test“ test.php简写:svn ci

登录后复制

4、加锁/解锁

svn lock -m “LockMessage“ [--force] PATH例如:svn lock -m “lock test file“ test.phpsvn unlock PATH

登录后复制

5、更新到某个版本

svn update -r m path例如:svn update如果后面没有目录,默认将当前目录以及子目录下的所有文件都更新到最新版本。svn update -r 200 test.php(将版本库中的文件test.php还原到版本200)svn update test.php(更新,于版本库同步。如果在提交的时候提示过期的话,是因为冲突,需要先update,修改文件,然后清除svn resolved,最后再提交commit)简写:svn up

登录后复制

6、查看文件或者目录状态

1)svn status path(目录下的文件和子目录的状态,正常状态不显示)【?:不在svn的控制中;M:内容被修改;C:发生冲突;A:预定加入到版本库;K:被锁定】M状态一般比较多2)svn status -v path(显示文件和子目录状态)第一列保持相同,第二列显示工作版本号,第三和第四列显示最后一次修改的版本号和修改人。注:svn status、svn diff和 svn revert这三条命令在没有网络的情况下也可以执行的,原因是svn在本地的.svn中保留了本地版本的原始拷贝。简写:svn st

登录后复制

7、删除文件

svn delete path -m “delete test fle“例如:svn delete svn://192.168.1.1/pro/domain/test.php -m “delete test file”或者直接svn delete test.php 然后再svn ci -m ‘delete test file‘,推荐使用这种简写:svn (del, remove, rm)

登录后复制

8、查看日志

svn log path例如:svn log test.php 显示这个文件的所有修改记录,及其版本号的变化

登录后复制

9、查看文件详细信息

svn info path例如:svn info test.php

登录后复制

10、比较差异

svn diff path(将修改的文件与基础版本比较)例如:svn diff test.phpsvn diff -r m:n path(对版本m和版本n比较差异)例如:svn diff -r 200:201 test.php简写:svn di

登录后复制

11、将两个版本之间的差异合并到当前文件

svn merge -r m:n path例如:svn merge -r 200:205 test.php(将版本200与205之间的差异合并到当前文件,但是一般都会产生冲突,需要处理一下)

登录后复制

12、SVN 帮助

svn helpsvn help ci

登录后复制

-------以上是常用命令,下面写几个不经常用的 -------

13、版本库下的文件和目录列表

svn list path显示path目录下的所有属于版本库的文件和目录简写:svn ls

登录后复制

14、创建纳入版本控制下的新目录

svn mkdir: 创建纳入版本控制下的新目录。用法: 1、mkdir PATH…2、mkdir URL…创建版本控制的目录。1、每一个以工作副本 PATH 指定的目录,都会创建在本地端,并且加入新增调度,以待下一次的提交。2、每个以URL指定的目录,都会透过立即提交于仓库中创建。在这两个情况下,所有的中间目录都必须事先存在。

登录后复制

15、恢复本地修改

svn revert: 恢复原始未改变的工作副本文件 (恢复大部份的本地修改)。revert:用法: revert PATH…注意: 本子命令不会存取网络,并且会解除冲突的状况。但是它不会恢复被删除的目录

登录后复制

16、代码库URL变更

svn switch (sw): 更新工作副本至不同的URL。用法: 1、switch URL [PATH] 2、switch –relocate FROM TO [PATH...] 1、更新你的工作副本,映射到一个新的URL,其行为跟“svn update”很像,也会将服务器上文件与本地文件合并。这是将工作副本对应到同一仓库中某个分支或者标记的方法。 2、改写工作副本的URL元数据,以反映单纯的URL上的改变。当仓库的根URL变动(比如方案名或是主机名称变动),但是工作副本仍旧对映到同一仓库的同一目录时使用这个命令更新工作副本与仓库的对应关系。

登录后复制

17、解决冲突

svn resolved: 移除工作副本的目录或文件的“冲突”状态。用法: resolved PATH…注意: 本子命令不会依语法来解决冲突或是移除冲突标记;它只是移除冲突的相关文件,然后让 PATH 可以再次提交。

登录后复制

18、输出指定文件或URL的内容

pyc一般不用替换,python自动检测pyc和py文件,如果pyc比py文件旧,会重新编译,理论上是这样

另外,如果程序在替换py文件之前就运行着,需要把程序关了,替换后再重开。