Get the App
SLTechnology News&Howtos  ›  Development  › 

How to output file names by directory level and save them as excel by Python

Shulou Source: shulou.com Published: 2022-06-01 12:02:10 09月26日 Update

This article is about how Python outputs file names at the directory level and saves them as excel. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

I. description of the problem

When we find that the memory of the computer is very full, or the folder management is not clear in our daily work, the file data inside is very messy and inconvenient to find, and it is a waste of time to look for a folder and then delete it!

2. Python saves the file name at the directory level 1. Source code

The code is as follows (example):

# encoding=utf-8import osimport xlwtfrom xlwt.Worksheet import Worksheetimport timepath = def find_dir_name:\ py\ python3.7\ test'file_dicts = {} # read directory name and file name as dictionary data (path Key=''): file_names = os.listdir (path) file_dict = {} for file_name in file_names: print ('current directory:% s'% path+'\\'+ file_name) file_ files [file _ name] = {} if os.path.isdir (path+'\'+ file_name): file_ files [file _ name] = find_dir_name (path+'\'+ file_name) File_name) else: pass return file_dict# stores dictionary data in Exceldef edit_excel (worksheet,data): # traversing dictionary data global row_number, row_data for key,value in data.items (): if value: row_data.append (key) # if value has a value Then iterate through edit_excel (worksheet,value) else: # if the value is empty, write key to sheet # to write excle col = 0 for name in row_data: print ('location:% dpene% d Value:% s'% (row_number,col,name)) worksheet.write (row_number,col,name) col = col+1 print ('location:% dline% d Value:% s'% (row_number,col,name) worksheet.write (row_number,col,key) row_number = row_number+1 if row_data: row_data.pop () # time.sleep (1) return Trueif _ _ name__ ='_ _ main__': # mm = str (find_dir_name (path,key)) # f = open ('file_name.txt') 'wb') # f.write (mm.encode (' utf-8')) # f.close () data = find_dir_name (path) # create worksheet workbook = xlwt.Workbook (encoding='utf-8') worksheet = workbook.add_sheet ('sheet1',cell_overwrite_ok=True) # temporarily record a row of data row_data = [] row_number = 0 edit_excel (worksheet Data) workbook.save ('converted excel file name .xls') 2. Running effect (partial screenshot)

Thank you for reading! On "Python how to output file names by directory level and save as excel" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, you can share it out for more people to see it!

Tags: File directory data file name hierarchy dictionary output location content folder more article work good practical messy one line code memory name Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn Huawei Shulou Information MySQL