我正努力完成以下任务:
traverse_dir()函数
- 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()函数
- read the excel file generated in the first function and create another excel file that will store only file names ending with '.apk'.下面是代码片段:
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.中,我可以列出子
在执行程序时,我会遇到以下错误。
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'为什么没有生成文件?有什么建议吗?
修改代码
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)发布于 2022-01-10 10:20:09
主要问题是if file_name in i:始终是假的,因此没有创建xlsx文件。您可能需要进行一些更改,以测试文件是否存在,例如:
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()https://stackoverflow.com/questions/70650372
复制相似问题