在mac中通过python读取文件夹中的文件
我想读取文件以查找文件的md5,并在mac中复制文件。 但是,我没有得到这样的文件或目录错误。我的代码如下在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
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)
在使用2md5bul(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: