我有一个目录,其中包含许多包含.csv文件的子目录。我的代码是这样的:
walk_dir = 'E:\grams'
print('walk_dir = ' + walk_dir)
walk_dir = os.path.abspath(walk_dir)
print('walk_dir (absolute) = ' + os.path.abspath(walk_dir))
for root, subdirs, files in os.walk(walk_dir):
print('--\nroot = ' + root)
list_file_path = os.path.join(root, 'my-directory-list.txt')
print('list_file_path = ' + list_file_path)
with open(list_file_path, 'wb') as list_file:
for subdir in subdirs:
print('\t- subdirectory ' + subdir)
for filename in files:
file_path = os.path.join(root, filename)
print('\t- file %s (full path: %s)' % (filename, file_path))
with open(file_path, 'rb') as f:
f_content = f.read()
df = pd.concat((pd.read_csv(f) for f in files))
运行时出现错误
FileNotFoundError: File b'my-directory-list.txt' does not exist
但是,如果我忽略了代码的最后一行(df = pd.concat((pd.read_csv(f) for f in files))
),脚本就可以正常工作。有什么想法吗?