【问题标题】:Record xls and xlsx file location from zip file extracted从提取的 zip 文件中记录 xls 和 xlsx 文件位置
【发布时间】:2020-05-19 19:44:07
【问题描述】:

下面的代码解压缩一个文件并将其放在一个目录中。有时 zip 文件在 zip 中包含文件夹。

代码工作我的问题是我还需要提取这些 zip 文件并将所有 xls 和 xlsx 文件的完整路径记录到数据框中,以便我可以遍历数据框并转换为 csv。 我曾尝试将文件路径存储在 df 中但没有成功的任何想法

processlist = sqldf[sqldf['filename'].isin(newlist)]

for file in processlist['filename']:
    source = rootpath + file
    dest = rootext + file.replace('.zip','')+"/"
    print(source)
    print(dest)
    with zipfile.ZipFile(source,'r') as zip_ref:
        zip_ref.extractall(dest)

【问题讨论】:

    标签: python excel


    【解决方案1】:

    为什么不循环播放目标文件夹中的所有文件和文件夹?

    您可以使用 os.walk 来完成该任务。见:

    https://www.tutorialspoint.com/python/os_walk.htm

    【讨论】:

    • 好吧,我有一个查找 xls 文件的函数,但它在路径形成方面存在问题。不知道发生了什么这是代码
    • def recursive_glog(treeroot,pattern): results = [] for base, dirs, files in os.walk(treeroot, topdown=True): goodfiles = fnmatch.filter(files, pattern) 结果。 extend(os.path.join(base, f) for f in goodfiles) 返回结果 recursive_glog(dest,'*.xls')
    • ['//RootFolder/datafeeds/CMS_ResearchStatistics/UnZipStageFile/monthly-contract-summary-report-april-2020/Monthly_Summary_Report_2020_04\\Monthly_Summary_Report_2020_04.xls']
    • 请注意最初输入的源变量的斜杠“/”,然后当它找到匹配时返回路径“\”的其余部分。我还在开头添加了“\\”。我需要到达这条路,这条路将无法正常工作
    • 您可以使用 os.join 来加入多个路径。它将根据您的操作系统使用分隔符。
    猜你喜欢
    • 2017-03-28
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多