[英]python checksum md5 with argv called from a main.py
參考我的代碼以從鏈接中的兩個來源檢查md5:
我分別獲得了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:]:
# 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)
想知道如何從其他python腳本開始構建菜單。
我的第一種方法是:
#!/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
似乎您想使用源和目標調用saca_sum_origen
,現在它只需要1個參數作為源。 該函數只需要修改以接受這些參數:
(我在這里簡化了一點)
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)
順便說一句,您似乎正在使用borrar = os.system('clear')
這行來為函數創建“捷徑”。 這一切都是將os.system('clear')
的輸出(什么都沒有)分配給變量borrar
,然后當您嘗試在菜單代碼中“調用”它時,實際上它什么也沒做。 如果您確實想創建一個“別名”函數,則可以使其成為一個函數: def borrar: os.system('clear')
,並且在調用它時不要忘記括號: borrar()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.