如果只处理Excel文件该怎么修改file_paths = glob.glob(os.path.join(folder_path, "*"))这段代码
时间: 2024-03-23 12:38:15 浏览: 130
如果只处理Excel文件,可以在glob.glob()的参数中加入文件后缀名,如下所示:
```
file_paths = glob.glob(os.path.join(folder_path, "*.xlsx"))
```
这样就只会匹配文件名后缀为.xlsx的Excel文件,而不会包括其他类型的文件。如果还要处理.xls文件,可以将参数修改为"*.xls*"。
相关问题
file_paths = glob.glob(os.path.join(folder_path, file_extension)) NameError: name 'glob' is not defined
This error occurs because the module `glob` has not been imported.
You can fix this error by adding the following line at the beginning of your code:
```python
import glob
```
This will import the `glob` module and make its functions available for use in your code.
def data_generator(): for folder in os.listdir("D:/wjd"): for file in os.listdir("D:/wjd/"+folder): file_paths = glob.glob('D:/wjd/*.png') for file_path in file_paths: img = Image.open(file_path) img = img.resize((224, 224)) img_arr = np.array(img) img_tensor = tf.convert_to_tensor(img_arr) img_tensor = tf.expand_dims(img_tensor, axis=0) print(img_tensor.shape) print(img_tensor.shape) # 输出张量的形状,我感觉不对,我应该删除那两行代码啊
是的,你是正确的。在这个数据生成器函数中,`print(img_tensor.shape)` 这行代码是不必要的,因为它只是输出张量的形状,而不是将它们返回给调用者。因此,你可以将这两行代码删除,如下所示:
```python
def data_generator():
for folder in os.listdir("D:/wjd"):
for file in os.listdir("D:/wjd/"+folder):
file_paths = glob.glob('D:/wjd/*.png')
for file_path in file_paths:
img = Image.open(file_path)
img = img.resize((224, 224))
img_arr = np.array(img)
img_tensor = tf.convert_to_tensor(img_arr)
img_tensor = tf.expand_dims(img_tensor, axis=0)
yield img_tensor
```
这样修改后,这个函数将会生成一个张量序列,每个张量代表一个图片。你可以将这个函数作为参数传递给你的模型,用于训练和测试。
阅读全文