我又卡住了! 我的故事是:
我需要查找目录中不同文件夹中存在的名为“tv.sas7bdat”的文件,并将找到的所有文件的内容保存到桌面上的单个 Excel 文件中。使用我的实际代码,我可以获取该文件的所有路径并将其内容传输到数据帧。但是,我无法将所有数据帧附加到一个 Excel 文件中。
在我的 Excel 中,我只找到最后一个数据框!!
这是我的代码,
import pandas as pd
from sas7bdat import SAS7BDAT
import os
path = "\\"
newpath = "\\"
files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
for file in f:
if 'tv.sas7bdat' in file:
files.append(os.path.join(r, file))
lenf = range(len(files))
for f in files:
print(f)
for df in lenf:
with SAS7BDAT(f) as file:
df = file.to_data_frame()
print(df)
group =pd.concat([df], axis=0, sort=True, ignore_index = True)
df.to_excel(newpath + 'dataframes_tv.xlsx',index=False)
最佳答案
您可能需要使用shutil
模块,它允许您使用copytree
函数复制目录及其中的文件。示例:
import shutil
shutil.copytree('/path/to/source', 'path/to/destination')
关于python - 将不同目录中的粘贴文件复制到一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56939239/