python列出带空格的文件

时间:2015-04-06 01:35:01

标签: python for-loop logging md5 spaces

我有以下代码:

基本上是从每个文件中提取md5。问题在于具有空格的文件,程序的解决方案可以考虑这些文件而不是跳过它们。

def onepath(archivo):
        logging.basicConfig(filename=salida,filemode="w", format='%(message)s', level=logging.DEBUG)
        for filename in (file for file in os.listdir(archivo)):
                with open(filename) as checkfile:
                        logging.info("MD5 " + "(%s) = " % filename + hashlib.md5(checkfile.read()).hexdigest())

我正在阅读有关shlex方法的内容,但不确定如何实现。

你能帮助我吗?


我认为带有空格的文件正在显示。我做了一个简短的片段而不是我面临的一个问题,我无法控制Linux如何理解文件名中的空格,以便如下所示:

files_destino = [f for f in os.listdir(os.path.join(sys.argv[1].strip()))]
for i in files_destino:
                print i
                subprocess.call(["cp","-v", "%s" % i,"/tmp/"])

在shell中显示:

bash-3.2$ ./comodin.py ./espacio/
Boxx view.pdf
cp: Boxx view.pdf: No such file or directory
hola.txt
hola.txt -> /tmp/hola.txt
bash-3.2$ 

1 个答案:

答案 0 :(得分:0)

def onepath(archivo):
    logging.basicConfig(filename=salida,filemode="w", format='%(message)s', level=logging.DEBUG)
    for filename in os.listdir(archivo):
        filepath = os.path.join(archivo, filename)
        with open(filepath) as checkfile:
            logging.info("MD5 " + "(%s) = " % filename + hashlib.md5(checkfile.read()).hexdigest())