Python呼び出しSVN実装ファイル一括rename
9476 ワード
Pythonを使用してosモジュールを呼び出し、import osはまずos.system()の使い方を見てみましょう.
subshellで指定したコマンド(このコマンドは文字列)を実行し、ステータス値を返します.
試してみる:
注意:dosコマンドラインに直接コマンドを入力するのとは異なり、コマンドに引用符''を付けて文字列にする必要があります.そうしないと、エラーが発生します.
次に、SVNのコマンドを呼び出す方法について説明します.たとえば、svnのヘルプ情報を表示します.
指定したファイルの名前を変更したいので、まずSVNのrenameコマンドの使い方を見てみましょう.
はい、move(mv,rename,ren):Move and/or rename something in working copy or repository.move(またはrename)コマンドを使用して移動し、名前を変更する操作が表示されます.もちろんmv,renを略記することもできます.
使用する前に、簡単な例で試してみましょう.
現在のディレクトリは(SVNの)作業コピーではありません!logコマンドの使い方を見てみましょう.
OK!logにコマンドを追加するパスは、設定されていない場合は現在のディレクトリです.次に、まず考えられるのは、コマンドの後ろにパスを付けることです.
しかし、複数回の操作を行うと、これでは不便です.パスを作業ディレクトリに切り替えたほうがいいです.では、DOSコマンドを使用して操作を続けます.
残念ながら現在の経路は変わっていません.OS.system呼び出しのコマンドはsubshellで実行されるため、python shellの現在のディレクトリは変更されません.ここではosモジュールのFUNCTIONを使用する必要があります. chdir(...) chdir(path) Change the current working directory to the specified path.
作業ディレクトリをrootpathに設定し、このディレクトリの下のTest 01_2013_11_15_01-001.txt名前をTest 01_に変更2013_11_15_01_001.txt :
はい、長い間忙しくて単一のファイルの名前を変えるだけで、これは絶対に私の初心ではありません.どうやって一括改名しますか?ファイル名を変数にあげてみますか?まず、ディレクトリの下にあるファイルを取得します.
次に、変更する文字と置換する文字を定義します.
戻り値は1で、操作に失敗しました.
renameコマンド実行時に変数old_をfileとnew_fileはファイル名ではなく普通の文字として扱われます.
解決策は簡単です.ファイル名変数が通常の文字伝達になる前に、renameコマンドを文字列に書きます. svnren=r'svn move '+old_file+' '+new_file os.system(svnren)
>>> 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に設定し、このディレクトリの下のTest 01_2013_11_15_01-001.txt名前をTest 01_に変更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)