使用Python调用os模块, import os
先看一下os.system()的用法:
>>> help(os.system) Help on built-in function system in module nt: system(...) system(command) -> exit_status Execute the command (a string) in a subshell. >>>
在subshell下执行给定的命令(这个命令是一个字符串),并返回状态值。
试一下:
>>> os.system('time')
注意,与直接在dos命令行输入命令不同,这里需要给命令加上引号'',使命令为一个字符串,否则会报错:
>>> os.system(time) Traceback (most recent call last): File "<pyshell#62>", line 1, in <module> os.system(time) NameError: name 'time' is not defined >>>
接下来就知道怎么调用SVN的命令了,例如,查看svn的帮助信息:
>>> os.system('svn help') 0
因为我想给指定的文件重命名,所以,先来查看一下SVN的rename命令怎么用:
>>> os.system('svn help rename') move (mv, rename, ren): Move and/or rename something in working copy or repository. usage: move SRC... DST When moving multiple sources, they will be added as children of DST, which must be a directory. Note: this subcommand is equivalent to a 'copy' and 'delete'. Note: the --revision option has no use and is deprecated. SRC and DST can both be working copy (WC) paths or URLs: WC -> WC: move and schedule for addition (with history) URL -> URL: complete server-side rename. All the SRCs must be of the same type. Valid options: -r [--revision] ARG : ARG (some commands also take ARG1:ARG2 range) A revision argument can be one of: NUMBER revision number '{' DATE '}' revision at start of the date 'HEAD' latest in repository 'BASE' base rev of item's working copy 'COMMITTED' last commit at or before BASE 'PREV' revision just before COMMITTED -q [--quiet] : print nothing, or only summary information --force : force operation to run --parents : make intermediate directories -m [--message] ARG : specify log message ARG -F [--file] ARG : read log message from file ARG --force-log : force validity of log message source --editor-cmd ARG : use ARG as external editor --encoding ARG : treat value as being in charset encoding ARG --with-revprop ARG : set revision property ARG in new revision using the name[=value] format Global options: --username ARG : specify a username ARG --password ARG : specify a password ARG --no-auth-cache : do not cache authentication tokens --non-interactive : do no interactive prompting --trust-server-cert : accept unknown SSL server certificates without prompting (but only with '--non-interactive') --config-dir ARG : read user configuration files from directory ARG --config-option ARG : set user configuration option in the format: FILE:SECTION:OPTION=[VALUE] For example: servers:global:http-library=serf 0
好的,可以看到:move (mv, rename, ren): Move and/or rename something in working copy or repository.
使用move(或者rename)命令来移动,重命名的操作。当然也可以用简写 mv,ren。
在使用之前先来用一个简单的例子来试探一下:
>>> os.system('svn log') svn:'.' is not a working copy 1
当前目录不是一个(SVN的)工作副本!看一下log命令的用法:
log: Show the log messages for a set of revision(s) and/or file(s). usage: 1. log [PATH] 2. log URL[@REV] [PATH...] 1. Print the log messages for a local PATH (default: '.'). The default revision range is BASE:1. 2. Print the log messages for the PATHs (default: '.') under URL. If specified, REV determines in which revision the URL is first looked up, and the default revision range is REV:1; otherwise, the URL is looked up in HEAD, and the default revision range is HEAD:1. Multiple '-c' or '-r' options may be specified (but not a combination of '-c' and '-r' options), and mixing of forward and reverse ranges is allowed. With -v, also print all affected paths with each log message. With -q, don't print the log message body itself (note that this is compatible with -v). Each log message is printed just once, even if more than one of the affected paths for that revision were explicitly requested. Logs follow copy history by default. Use --stop-on-copy to disable this behavior, which can be useful for determining branchpoints. Examples: svn log svn log foo.c svn log http://www.example.com/repo/project/foo.c svn log http://www.example.com/repo/project foo.c bar.c Valid options: -r [--revision] ARG : ARG (some commands also take ARG1:ARG2 range) A revision argument can be one of: NUMBER revision number '{' DATE '}' revision at start of the date 'HEAD' latest in repository 'BASE' base rev of item's working copy 'COMMITTED' last commit at or before BASE 'PREV' revision just before COMMITTED -q [--quiet] : print nothing, or only summary information -v [--verbose] : print extra information -g [--use-merge-history] : use/display additional information from merge history -c [--change] ARG : the change made in revision ARG --targets ARG : pass contents of file ARG as additional args --stop-on-copy : do not cross copies while traversing history --incremental : give output suitable for concatenation --xml : output in XML -l [--limit] ARG : maximum number of log entries --with-all-revprops : retrieve all revision properties --with-no-revprops : retrieve no revision properties --with-revprop ARG : retrieve revision property ARG Global options: --username ARG : specify a username ARG --password ARG : specify a password ARG --no-auth-cache : do not cache authentication tokens --non-interactive : do no interactive prompting --trust-server-cert : accept unknown SSL server certificates without prompting (but only with '--non-interactive') --config-dir ARG : read user configuration files from directory ARG --config-option ARG : set user configuration option in the format: FILE:SECTION:OPTION=[VALUE] For example: servers:global:http-library=serf
OK!log后面加命令的路径,如果没有设置,就是当前目录。那么接下来,首先想到的是在命令后面加上路径:
>>> os.system('svn log E:\test')
但是如果要进行多次操作的话,这样就很不方便。最好是把路径切换到我们的工作目录。那么继续使用DOS命令来操作:
>>> os.system('E:') 0 >>> os.getcwd() 'C:\\Python26' >>>
很遗憾当前路径并没有改变。因为os.system调用的命令是在subshell下执行的,而python shell的当前目录并没有改变,这里需要用到
os模块中的FUNCTION:
chdir(...)
chdir(path)
Change the current working directory to the specified path.
>>> os.chdir('E:\test') >>> print os.getcwd() E:\test
好了,我们把工作目录设定为rootpath,并且把此目录下的 Test01_2013_11_15_01-001.txt 重命名为 Test01_2013_11_15_01_001.txt :
>>> rootpath=r'E:\test' >>> os.system('svn move Test01_2013_11_15_01-001.txt Test01_2013_11_15_01_001.txt') A Test01_2013_11_15_01_001.txt D Test01_2013_11_15_01-001.txt 0
好了,忙了半天只让单个文件重命名,这绝对不是我的初衷。如何批量改名呢?把文件名给变量试试?
首先获取目录下的文件:
>>> filenames = os.listdir(path)
然后定义想要改变的字符以及替换的字符:
>>> source_char = raw_input('Enter the characters that you want to modify:') Enter the characters that you want to modify:-0 >>> copy_char = raw_input('Enter the characters that you want to get:') Enter the characters that you want to get:_0 >>> for old_file in filenames: new_file = string.replace(old_file,source_char,copy_char) os.system('svn move old_file new_file') 1 1 1 1 1 1 1 1 1 1 1 1 1
返回值为1,操作失败!
因为在rename 命令执行时,把变量old_file 和 new_file当做了普通的字符,而不是某个文件名。
解决方法很简单,就是在文件名变量变成普通字符传之前,把rename命令写成字符串:
svnren=r'svn move '+old_file+' '+new_file
os.system(svnren)