只显示process finished with excit code0
各位,我编写了一串代码,意图是:在G:\桌面\1这个文件夹中搜索标题中含有“1”的excel文件。注意这个文件夹中有一堆excel其中有些文件格式是.xlsx有些是.xls。把找到的这个excel的完整标题打印出来。
GPT给我的代码是:
import os
import glob
import xlrd
# Define the directory to search in
dir_path = r'G:\桌面\1'
# Define the search query
query = '1'
# Search for Excel files in the directory
excel_files = glob.glob(os.path.join(dir_path, '*.xls*'))
# Loop through the Excel files and search for the query
for file_path in excel_files:
# Open the Excel file
try:
workbook = xlrd.open_workbook(file_path)
except (xlrd.biffh.XLRDError, PermissionError):
continue
# Get the first worksheet
worksheet = workbook.sheet_by_index(0)
# Check if the query is in the title or content
if query in os.path.basename(file_path) or query in worksheet.name or query in str(worksheet.cell_value(0, 0)):
# Print the title of the file
print(os.path.basename(file_path))
结果却打印不出文件名只是显示: Process finished with exit code 0
这是为何???说是运行成功了但是却并不执行???
|