首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么蟒蛇熊猫不能生成xlsx文件?

为什么蟒蛇熊猫不能生成xlsx文件?
EN

Stack Overflow用户
提问于 2022-01-10 09:41:33
回答 1查看 66关注 0票数 -2

我正努力完成以下任务:

traverse_dir()函数

代码语言:javascript
复制
- read a root directory, and get the names of the sub directories.
- read the sub directories and see if 'installed-files.json' file is present.
- if the 'installed-files.json' file is present in all the directories, then open them and create a excel file out of the JSON file those are present in all the sub directories.

filter_apk()函数

代码语言:javascript
复制
- read the excel file generated in the first function and create another excel file that will store only file names ending with '.apk'.

下面是代码片段:

代码语言:javascript
复制
def traverse_dir(rootDir, file_name):
    dir_names = []
    for names in os.listdir(rootDir):
        entry_path = os.path.join(names)
        if os.path.isdir(entry_path):
            dir_names.append(entry_path)
            for i in dir_names:
                if file_name in i:
                    with open(file_name) as jf:
                        data = json.load(jf)
                        df = pd.DataFrame(data)
                        new_df = df[df.columns.difference(['SHA256'])]
                        new_df.to_excel('abc.xlsx')

def filter_apk():
    traverse_dir(rootDir, file_name)
    old_xl = pd.read_excel('abc.xlsx')
    a = old_xl[old_xl["Name"].str.contains("\.apk")]
    a.to_excel('zybg.xlsx')

rootDir = '<root path where sub folders resides>'
file_name = 'installed-files.json'
filter_apk()

注意:

  • --我已经分别在单个文件夹上测试了代码,它的工作原理就像魅力一样。只有当我尝试使用多个目录时才会面临问题。

实际上,在第一个函数directories.中,我可以列出子

在执行程序时,我会遇到以下错误。

代码语言:javascript
复制
Traceback (most recent call last):
  File "Jenkins.py", line 36, in <module>
    filter_apk()
  File "Jenkins.py", line 30, in filter_apk
    old_xl = pd.read_excel('abc.xlsx')

    with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'abc.xlsx'

为什么没有生成文件?有什么建议吗?

修改代码

代码语言:javascript
复制
def traverse_dir(rootDir, file_name):
    dir_names = []
    for names in os.listdir(rootDir):
        entry_path = os.path.join(rootDir, names)
        if os.path.isdir(entry_path):
            dir_names.append(entry_path)
            for fil_name in dir_names:
                file_path = os.path.join(entry_path, fil_name, file_name)
                print(file_path)
                if os.path.isfile(file_path):
                    with open(file_path) as jf:
                        data = json.load(jf)
                        df = pd.DataFrame(data)
                        df1 = pd.DataFrame(data)
                        new_df = df[df.columns.difference(['SHA256'])]
                        new_df1 = df1[df.columns.difference(['SHA256'])]
                        with pd.ExcelWriter('abc.xlsx') as writer:
                            new_df.to_excel(writer, sheet_name='BRA', index=False)
                            new_df1.to_excel(writer, sheet_name='CNA', index=False)
                else:
                    raise FileNotFoundError

rootDir = <path to subdirs
file_name = 'installed-files.json'
traverse_dir(rootDir, file_name)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-10 10:20:09

主要问题是if file_name in i:始终是假的,因此没有创建xlsx文件。您可能需要进行一些更改,以测试文件是否存在,例如:

代码语言:javascript
复制
import os
def traverse_dir(rootDir, file_name):
    dir_names = []
    for names in os.listdir(rootDir):
        entry_path = os.path.join(names)
        if os.path.isdir(entry_path):
            dir_names.append(entry_path)
            for i in dir_names:
                file_path=os.path.join(rootDir,i,file_name)
                if os.path.isfile(file_path):
                    with open(file_path) as jf:
                        data = json.load(jf)
                        df = pd.DataFrame(data)
                        new_df = df[df.columns.difference(['SHA256'])]
                        new_df.to_excel('abc.xlsx')

def filter_apk():
    traverse_dir(rootDir, file_name)
    old_xl = pd.read_excel('abc.xlsx')
    a = old_xl[old_xl["Name"].str.contains("\.apk")]
    a.to_excel('zybg.xlsx')

rootDir = '<root path where sub folders resides>'
file_name = 'installed-files.json'
filter_apk()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70650372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档