我使用Python 2.7刚刚开始的路径,并使用下面的代码,以确定文件路径:os.walk查找到文件的问题(Python 2.7版)
import os, fnmatch
#find the location of sunnyexplorer.exe
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename
for filename in find_files('c:\users','*.sx2'):
print ('Found Sunny Explorer data in:', filename)
寄托都似乎运作直到我尝试使用该路径并发现错误。
c:\users\woody\Documents\SMA\Sunny Explorer
,而正确的路径是:
c:\users\woody\My Documents\SMA\Sunny Explorer
这里的代码中没有任何东西会产生不正确的路径。 'os.walk()'使用'os.listdir()',没有别的,所以你的操作系统报告那里有一个'Documents'文件夹。你确定'woody'文件夹中有* no *'Documents'文件夹吗? –
'我的文档'在Windows Vista和7中被称为'文档',只是资源管理器显示不同的名称。 – filmor