我有一些代码可以搜索与某个关键字匹配的网络共享中的文件。当找到匹配项时,我想将找到的文件复制到网络上的其他位置。我得到的错误如下:无法使用os.walk来解析路径
Traceback (most recent call last):
File "C:/Users/user.name/PycharmProjects/SearchDirectory/Sub-Search.py", line 15, in <module>
shutil.copy(path+name, dest)
File "C:\Python27\lib\shutil.py", line 119, in copy
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: '//server/otheruser$/Document (user).docx'
我相信这是因为我想找到的文件复制,而不指定它的直接路径,因为一些文件的子文件夹中找到。如果是这样,当它与关键字匹配时,如何将文件的直接路径存储到文件中?这里是我到目前为止的代码:
import os
import shutil
dest = '//dbserver/user.name$/Reports/User'
path = '//dbserver/User$/'
keyword = 'report'
print 'Starting'
for root, dirs, files in os.walk(path):
for name in files:
if keyword in name.lower():
shutil.copy(path+name, dest)
print name
print 'Done'
PS。被访问的用户文件夹是隐藏的,因此是$。
我编辑了标题,使这个问题更有可能出现在Google搜索中。我不认为网络份额在这里特别重要 –