在子进程stdout中添加逗号



python2.6-linux-centos6.7我有一个python脚本,可以实现我想要的东西。我使用子进程、UNIXfind和xargs来获取多个文件的md5sum。然后我把这个输出写到一个文件中,我遇到的问题是用逗号分隔哈希和文件名之间的空间。我尝试过拆分,但没有成功,还玩过csv模块。请建议:

parser = optparse.OptionParser()
parser.add_option('--searchpath', action="store", default="~", help="Enter your search path for files", type="string")
parser.add_option('--extension', action="store", default="*.php", help="Enter your file extension", type="string")
options, args = parser.parse_args()
if len(sys.argv[1:]) == 0:
    print "no argument given!"
    parser.print_help()
    sys.exit(1)
CMD = "/bin/find %s -name *.%s | xargs md5sum" % (options.searchpath,options.extension)
out = subprocess.Popen(CMD,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
(out,err) = out.communicate()
fp = open('logfile.txt','wb')
fp.write(out)
fp.close()

请查看输出:

[root@jr-sandbox python_scripts]# cat logfile.txt
afc69261af7ca0700185e4d9085e7181  /root/python_scripts/testfiles/yum_save_tx-2015-11-11-16-57y4XwNP.yumtx
0662322d5529501d09c4cce1371d2db3  /root/python_scripts/testfiles/yum_save_tx-2015-11-12-13-438AWQ5C.yumtx
55c04f8add81c667128398cb2f6d258d  /root/python_scripts/testfiles/yum_save_tx-2015-11-12-11-26gCDt0O.yumtx
1f7c612e4a00c53bb8ee7a1f0917aaad  /root/python_scripts/testfiles/yum_save_tx-2015-10-28-16-149rr0Ad.yumtx
690e3ccf94b75e9960ee3bd2a1f23bce  /root/python_scripts/testfiles/yum_save_tx-2015-03-16-13-42SAoXeV.yumtx

期望结果:

[root@jr-sandbox python_scripts]# cat logfile.txt
afc69261af7ca0700185e4d9085e7181 , /root/python_scripts/testfiles/yum_save_tx-2015-11-11-16-57y4XwNP.yumtx
0662322d5529501d09c4cce1371d2db3 , /root/python_scripts/testfiles/yum_save_tx-2015-11-12-13-438AWQ5C.yumtx
55c04f8add81c667128398cb2f6d258d , /root/python_scripts/testfiles/yum_save_tx-2015-11-12-11-26gCDt0O.yumtx
1f7c612e4a00c53bb8ee7a1f0917aaad , /root/python_scripts/testfiles/yum_save_tx-2015-10-28-16-149rr0Ad.yumtx
690e3ccf94b75e9960ee3bd2a1f23bce , /root/python_scripts/testfiles/yum_save_tx-2015-03-16-13-42SAoXeV.yumtx

在脚本中重新加载文件.replace(" ", " , ")

解决问题?

最新更新