Python 搜索本机全部excel文件并返回文件名
时间: 2023-04-03 10:04:44 浏览: 98
你可以使用 Python 的 os 和 glob 模块来搜索本机全部 excel 文件并返回文件名。具体代码如下:
```python
import os
import glob
# 搜索当前目录及其子目录下所有的 excel 文件
excel_files = glob.glob(os.path.join(os.getcwd(), '**/*.xlsx'), recursive=True)
# 输出所有 excel 文件的文件名
for file in excel_files:
print(os.path.basename(file))
```
这段代码会搜索当前目录及其子目录下所有的 .xlsx 文件,并输出它们的文件名。如果你想搜索 .xls 文件,只需要将 glob.glob() 中的参数改为 `'**/*.xls'` 即可。
相关问题
使用python的pandas读取excel文件内容并打印
以下是使用Python的pandas库读取Excel文件内容并打印的示例代码:
```python
import pandas as pd
# 读取Excel文件
df = pd.read_excel('data.xlsx')
# 打印读取的数据
print(df)
```
其中,`data.xlsx`是要读取的Excel文件名,`pd.read_excel()`函数用于读取Excel文件,返回一个DataFrame对象,可以直接打印输出。如果Excel文件中有多个sheet,可以通过指定sheet_name参数来指定要读取的sheet名称或索引。
django下载excel文件无文件名
可以通过设置HTTP头来指定下载文件的文件名。在Django中,可以使用HttpResponse对象来设置HTTP头。以下是一个示例代码:
```python
import csv
from django.http import HttpResponse
def download_csv(request):
# 生成CSV文件内容
csv_content = "1,2,3\n4,5,6\n7,8,9"
# 创建HttpResponse对象
response = HttpResponse(csv_content, content_type='text/csv')
# 设置HTTP头,指定下载文件的文件名
response['Content-Disposition'] = 'attachment; filename="data.csv"'
return response
```
在这个示例中,我们使用HttpResponse对象来返回CSV文件的内容,并设置了Content-Disposition头来指定下载的文件名为"data.csv"。你可以根据你的实际需求来修改文件名。