Python - 与路径相关的错误

时间:2017-09-15 03:08:16

标签: python jupyter-notebook

我正在使用此处的代码:

http://pythoncentral.io/finding-duplicate-files-with-python/

查找文件夹中的重复文件。

这是我在Python中的第一步(我来自VBA for Excel),我的问题可能非常简单,但我尝试了几件事但没有成功。运行代码后,我收到消息:

-f is not a valid path, please verify
An exception has occurred, use %tb to see the full traceback.

%tb生成:

SystemExit                                Traceback (most recent call last)
<ipython-input-118-31268a802b4a> in <module>()
     11             else:
     12                 print('%s is not a valid path, please verify' % i)
---> 13                 sys.exit()
     14         printResults(dups)
     15     else:

SystemExit: 

我使用的代码是:

# dupFinder.py
import os, sys
import hashlib

def findDup(parentFolder):
    # Dups in format {hash:[names]}
    dups = {}
    for dirName, subdirs, fileList in os.walk(parentFolder):
        print('Scanning %s...' % dirName)
        for filename in fileList:
            # Get the path to the file
            path = os.path.join(dirName, filename)
            # Calculate hash
            file_hash = hashfile(path)
            # Add or append the file path
            if file_hash in dups:
                dups[file_hash].append(path)
            else:
                dups[file_hash] = [path]
    return dups


# Joins two dictionaries
def joinDicts(dict1, dict2):
    for key in dict2.keys():
        if key in dict1:
            dict1[key] = dict1[key] + dict2[key]
        else:
            dict1[key] = dict2[key]


def hashfile(path, blocksize = 65536):
    afile = open(path, 'rb')
    hasher = hashlib.md5()
    buf = afile.read(blocksize)
    while len(buf) > 0:
        hasher.update(buf)
        buf = afile.read(blocksize)
    afile.close()
    return hasher.hexdigest()


def printResults(dict1):
    results = list(filter(lambda x: len(x) > 1, dict1.values()))
    if len(results) > 0:
        print('Duplicates Found:')
        print('The following files are identical. The name could differ, but the content is identical')
        print('___________________')
        for result in results:
            for subresult in result:
                print('\t\t%s' % subresult)
            print('___________________')

    else:
        print('No duplicate files found.')


if __name__ == '__main__':
path='C:/DupTestFolder/' #this is the path to analyze for duplicated files
    if len(sys.argv) > 1:
        dups = {}
        folders = sys.argv[1:]
        for i in folders:
            # Iterate the folders given
            if os.path.exists(i):
                # Find the duplicated files and append them to the dups
                joinDicts(dups, findDup(i))
            else:
                print('%s is not a valid path, please verify' % i)
                sys.exit()
        printResults(dups)
    else:
        print('Usage: python dupFinder.py folder or python dupFinder.py folder1 folder2 folder3')

我尝试使用和不使用&#34; \&#34;结束路径。最后,但结果是一样的。

我正在使用Python 3运行Jupyter。

提前感谢您的帮助!

3 个答案:

答案 0 :(得分:0)

您的代码中未使用路径变量。

您所做的只是对sys.argv[1:]的迭代,这是您脚本的参数。您将每个参数视为目录路径。

在Windows控制台上,您可以尝试:

python dupFinder.py C:\DupTestFolder

它应该有用。

答案 1 :(得分:0)

Sys.argv在命令行窗口中工作并接受参数。它自然不适用于jupyter笔记本,或者您需要在jupyter笔记本中找出一些命令。

答案 2 :(得分:0)

谢谢!,我现在可以运行代码了。我必须做两件事:

  1. 将dupFinder.py保存到运行我的python安装的同一文件夹中,在我的情况下为C:\ Users \ Pepe
  2. 从Anaconda打开cmd窗口(在运行python的文件夹中创建cmd窗口),我认为我可以同样打开命令窗口并导航(cd \ command)到文件夹位置
  3. 最后运行python dupFinder.py C:\ DupTestFolder。
  4. 现在我需要了解如何将结果保存到.txt文件以供将来使用,我会在发布之前搜索它。谢谢你的帮助!