我正在尝试使用&rs; rsync'来读取要同步的文件的总数,并使用以下python代码读取值,我得到以下输出。我应修改哪些代码才能获得所需的输出
B' 10'
10
`public class abc //anything but NOT STRING cuase it is built in
{
public static void main(String args[])
{
String a1="hello";
String a2="there";
String a3="how";
String a4="are";
String a5="you";
System.out.println(a1+a2+a3+a4+a5);
}
}`
rsync -nvaz --delete --stats user@host:/www/ . | ./awk.sh
awk '\
BEGIN {count = 0}
/deleting/ {if ( length($1) > 0 ) ++count} \
/Number of regular files transferred: / {count += $6} \
END \
{
printf "%d",count
}'
答案 0 :(得分:0)
您的awk
脚本只是寻找包含字符串然后打印它的行。既然你的python脚本需要读取stdout以获得该值,你也可以放弃脚本并坚持使用python。使用Popen
对象,您可以逐行读取标准输出
import subprocess
# for test...
source_dir = 'test1/'
target_dir = 'test2/'
count = 0
proc = subprocess.Popen(['rsync', '-nvaz', '--delete', '--stats',
source_dir, target_dir],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
for line in proc.stdout:
if line.startswith(b'Number of regular files transferred:'):
count = int(line.split(b':')[1])
proc.wait()
print(count)
答案 1 :(得分:0)
将输出解码为 utf-8 ,然后使用RegEx解析
o = subprocess.check_output(cmd, shell=True)
g = re.search(r'count=(\d+)', o.decode("utf-8"), re.M|re.I)