Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在mac中通过python读取文件夹中的文件_Python_Macos_Filesystems - Fatal编程技术网

在mac中通过python读取文件夹中的文件

在mac中通过python读取文件夹中的文件,python,macos,filesystems,Python,Macos,Filesystems,我想读取文件以查找文件的md5,并在mac中复制文件。 但是,我没有得到这样的文件或目录错误。我的代码如下 import os import hashlib def md5bul (dosyayolu): try: BLOCKSIZE = 65536 hasher = hashlib.md5() with open(dosyayolu, 'rb' 'utf8') as afile: buf = afile.read

我想读取文件以查找文件的md5,并在mac中复制文件。 但是,我没有得到这样的文件或目录错误。我的代码如下

import os
import hashlib

def md5bul (dosyayolu):
    try:
        BLOCKSIZE = 65536
        hasher = hashlib.md5()
        with open(dosyayolu, 'rb' 'utf8') as afile:
            buf = afile.read(BLOCKSIZE)
            while len(buf) > 0:
                hasher.update(buf)
                buf = afile.read(BLOCKSIZE)
        print(hasher.hexdigest())
    except IOError, e:
        print  ' Error %s' %e 


root = '/Users/username/'
for path, subdirs, files in os.walk(root):
    for name in files:
        dosya = path+name
        md5bul (path,name)
        print os.path.join(path, name)
with open(os.path.join (path,name), 'rb' 'utf8') as afile:
但是,我得到以下错误消息

[Errno 2] No such file or directory: 
我不明白为什么我会收到这个错误消息


你意识到代码中有错误吗?

很明显,
dosya=path+name
是不正确的,因为
os中的
path
。walk
没有尾随斜杠,比如:/Users/username,所以添加文件名“test”会变成Users/usernametest,这是不正确的

其次,您的
md5bul(dosyayolu)
在使用2
md5bul(path,name)
调用您时使用一个参数,这就是它抛出file not found错误的原因

您可以更改为呼叫:

mdfbul(os.path.join(path, name))
但是

输出为“/Users/username/Desktop/C”

其次,是的,你是对的,但我粘贴了错误的代码。
通常,我将这行代码写为md5bul(dosyayolu)

ok@anzel我解决了这个问题,谢谢。 我改变我的代码如下

import os
import hashlib

def md5bul (dosyayolu):
    try:
        BLOCKSIZE = 65536
        hasher = hashlib.md5()
        with open(dosyayolu, 'rb' 'utf8') as afile:
            buf = afile.read(BLOCKSIZE)
            while len(buf) > 0:
                hasher.update(buf)
                buf = afile.read(BLOCKSIZE)
        print(hasher.hexdigest())
    except IOError, e:
        print  ' Error %s' %e 


root = '/Users/username/'
for path, subdirs, files in os.walk(root):
    for name in files:
        dosya = path+name
        md5bul (path,name)
        print os.path.join(path, name)
with open(os.path.join (path,name), 'rb' 'utf8') as afile: