python checksum md5与argv调用从main.py [英] python checksum md5 with argv called from a main.py
问题描述
引用我的代码,在我的链接中从两个来源检查md5:
$ b $我实现得到md5分别。 (总是欢迎任何改进),这里是我的代码:
#!/ usr / bin / env python
import logging
import hashlib
import os
import sys
from sys import *
import subprocess
#script,path, path2 = argv
outfile =md5_origen.txt
outfile2 =md5_destino.txt
cmdargs = sys.argv
total = len(sys.argv) -1
#EJEMPLO PARA SACAR LOS ARGUMENTOS
###############
#for a in cmdargs [1:]:
#打印a
################
def saca_sum_origen(y):
#si cambia de directorio ,que cambio de archivo para despues ser评估。
如果一个!= sys.argv [total]:
ck =md5%s /%s%(a,y)
p = subprocess.Popen(ck,stdout = subprocess。 PIPE,shell = True)
(output,err)= p.communicate()
with open(outfile,'a')as text_file:
text_file.write(%s%输出)
else:
ck =md5%s /%s%(a,y)
p = subprocess.Popen(ck,stdout = subprocess.PIPE,shell = True)
(output,err)= p.communicate()
with open(outfile2,'a')as text_file:
text_file.write(%s%output)
#obtenemos los argumentos
for a in cmdargs [1:]:
#esto es que cada directorio enliste los files que tiene adentro
for x in(file for file in os。 listdir(a)):
如果不是〜在x:
#que obtenga su MD5
saca_sum_origen(x)$ b想知道如何从其他python脚本开始构建一个菜单。
$ b p>
我的第一种方法是:
#!/ usr / bin / env python
# - * - 编码:utf-8 - * -
import os
from sys import *
import sys
import subprocess
import cksum_v2
borrar = os.system('clear')
opcion = True
while opcion:
print选择一个选项:\\\
print1。
try:
opcion = int(raw_input(> _))
如果opcion == 1:
printJot您的输入文件夹
origen = raw_input()
print现在您的输出文件夹
destino = raw_input()
subprocess.call([./ cksum_v2.py ,origen,destino])
borrar
打印完成!
打印¿想要其他? y / n
try:
descicion = str(raw_input(> _))
如果descicion ==y:
opcion = True
elif descicion ==n:
printBYE
opcion = False
else:
打印ADIOS !!!
opcion = False
除了:
borrar
打印BYE
opcion = False
elif opcion> 1或opcion< 4:
os.system 'clear')
print正在建设中
opcion = True
elif opcion> 5:
print不存在该选项,另一个?
opcion = True
除了:
print不要生气了,BYE touchy !!
opcion = False
解决方案 看起来你想要调用一个源代码和一个目的地 saca_sum_origen
,现在它只需要一个参数为源。该功能只需要修改即可获得这些参数:
(我简化了一下这里)
def saca_sum_origen(source,dest):
ck =md5%s%source
p = subprocess.Popen(ck,stdout = subprocess.PIPE,shell = True )
(output,err)= p.communicate()
with open(dest,'a')as text_file:
text_file.write(str(output))
然后只需替换这行 subprocess.call([./ cksum_v2.py,origen, destino])
与 cksum_v2.saca_sum_origen(origen,destino)
顺便说一下看来你正在试图为这个行的一个函数code> borrar = os.system('clear')做一个快捷方式。所有这些都是将 os.system('clear')
的输出分配给变量 borrar
,然后当您尝试在菜单代码中调用它时,实际上并没有做任何事情。如果你真的想创建一个别名功能,你可以使它成为一个功能: def borrar:os.system('clear')
,不要忘记你调用它时的括号: borrar()
Is reference with my code to check the md5 from two sources in my link:
python saving output from a for iteration and subprocess for checksum
I achieve getting md5 respectively. (Any improvements are always welcome) here is my code:
#!/usr/bin/env python
import logging
import hashlib
import os
import sys
from sys import *
import subprocess
#script, path, path2 = argv
outfile = "md5_origen.txt"
outfile2 = "md5_destino.txt"
cmdargs = sys.argv
total = len(sys.argv) -1
#EJEMPLO PARA SACAR LOS ARGUMENTOS
################
#for a in cmdargs[1:]:
# print a
################
def saca_sum_origen(y):
#si cambia de directorio, que cambio de archivo para despues ser evaluado.
if a != sys.argv[total]:
ck = "md5 %s/%s" % (a,y)
p = subprocess.Popen(ck, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
with open(outfile,'a') as text_file:
text_file.write("%s" % output)
else:
ck = "md5 %s/%s" % (a,y)
p = subprocess.Popen(ck, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
with open(outfile2,'a') as text_file:
text_file.write("%s" % output)
#obtenemos los argumentos
for a in cmdargs[1:]:
#esto es que cada directorio enliste los files que tiene adentro
for x in (file for file in os.listdir(a)):
if not "~" in x:
#que obtenga su MD5
saca_sum_origen(x)
Wondering how can I start building a menu from an other python script.
My first approach is the following:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from sys import *
import sys
import subprocess
import cksum_v2
borrar = os.system('clear')
opcion = True
while opcion:
print "Select an option: \n"
print "1. Create a md5 report from source and target only"
try:
opcion = int(raw_input(">_ "))
if opcion == 1:
print "Jot down your input folder"
origen = raw_input()
print "Now your output folder"
destino = raw_input()
subprocess.call(["./cksum_v2.py", origen, destino])
borrar
print "Done!"
print "¿Want an other? y/n"
try:
descicion = str(raw_input(">_ "))
if descicion == "y":
opcion = True
elif descicion == "n":
print "BYE"
opcion = False
else:
print "ADIOS!!!"
opcion = False
except:
borrar
print "BYE"
opcion = False
elif opcion >1 or opcion <4:
os.system('clear')
print "Under construction"
opcion = True
elif opcion >5:
print "Doesnt exist that option, an other?"
opcion = True
except:
print "DOnt get mad, BYE touchy!!"
opcion = False
解决方案 It looks like you want to call saca_sum_origen
with a source and a destination, and right now it just takes 1 argument for the source. The function just needs to be modified to take those arguments:
(I simplified it a bit here)
def saca_sum_origen(source, dest):
ck = "md5 %s" % source
p = subprocess.Popen(ck, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
with open(dest,'a') as text_file:
text_file.write(str(output))
Then just replace this line subprocess.call(["./cksum_v2.py", origen, destino])
with cksum_v2.saca_sum_origen(origen, destino)
By the way, it looks like you're trying to make a "shortcut" for a function with this line borrar = os.system('clear')
. All this does is assigns the output of os.system('clear')
, (which is nothing) to the variable borrar
, and then when you're trying to "call" it within your menu code, it's actually not doing anything. If you really want to create an "alias" function, you can make it a function: def borrar: os.system('clear')
, and don't forget the parenthesis when you call it: borrar()
这篇关于python checksum md5与argv调用从main.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文