python 带有从 main.py 调用的 argv 的校验和 md5

python checksum md5 with argv called from a main.py

参考我的代码来检查我 link 中两个来源的 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') 为函数创建 "shortcut"。所有这些所做的就是将 os.system('clear') 的输出(什么都没有)分配给变量 borrar,然后当你试图在你的菜单代码中 "call" 它时,它实际上不是做任何事情。如果你真的想创建一个"alias"函数,你可以把它做成一个函数:def borrar: os.system('clear'),调用的时候别忘了括号:borrar()