if not os.path.exists(out_path): os.makedirs(out_path) CSVs = glob.glob(os.path.join(file_path,"*.csv"))
时间: 2024-05-02 16:14:28 浏览: 15
以下是一个关于如何检查并创建文件夹以及如何使用glob模块获取指定路径下的所有csv文件的示例代码:
```python
import os
import glob
# 检查并创建文件夹
if not os.path.exists(out_path):
os.makedirs(out_path)
# 使用glob模块获取指定路径下的所有csv文件
CSVs = glob.glob(os.path.join(file_path, "*.csv"))
```
在这个示例中,首先使用`os.path.exists()`函数检查指定路径`out_path`是否存在,如果不存在则使用`os.makedirs()`函数创建该文件夹。然后使用`glob.glob()`函数结合`os.path.join()`函数获取指定路径`file_path`下的所有以`.csv`为后缀的文件。
相关问题
if not os.path.exists(LOG_PATH):
如果你想检查 `LOG_PATH` 路径是否存在并执行相应的操作,你可以使用以下代码:
```python
import os
if not os.path.exists(LOG_PATH):
# 执行你想要的操作,例如创建文件夹或者进行其他处理
os.makedirs(LOG_PATH)
```
在这个例子中,`os.path.exists(LOG_PATH)` 用于检查路径是否存在。如果路径不存在,`os.makedirs(LOG_PATH)` 可以用来创建该路径。你可以在这个条件下执行你想要的操作。
将下面代码简洁化:def split_dataset(img_path, target_folder_path, output_path): filename = [] total_imgs = os.listdir(img_path) #for root, dirs, files in os.walk(img_path): for img in total_imgs: filename.append(img) np.random.shuffle(filename) train = filename[:int(len(filename) * 0.9)] test = filename[int(len(filename) * 0.9):] out_images = os.path.join(output_path, 'imgs') if not os.path.exists(out_images): os.makedirs(out_images) out_images_train = os.path.join(out_images, 'training') if not os.path.exists(out_images_train): os.makedirs(out_images_train) out_images_test = os.path.join(out_images, 'test') if not os.path.exists(out_images_test): os.makedirs(out_images_test) out_annotations = os.path.join(output_path, 'annotations') if not os.path.exists(out_annotations): os.makedirs(out_annotations) out_annotations_train = os.path.join(out_annotations, 'training') if not os.path.exists(out_annotations_train): os.makedirs(out_annotations_train) out_annotations_test = os.path.join(out_annotations, 'test') if not os.path.exists(out_annotations_test): os.makedirs(out_annotations_test) for i in train: print(os.path.join(img_path, i)) print(os.path.join(out_images_train, i)) shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_train, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_train, annotations_name)) for i in test: shutil.copyfile(os.path.join(img_path, i), os.path.join(out_images_test, i)) annotations_name = "gt_" + i[:-3] + 'txt' shutil.copyfile(os.path.join(target_folder_path, annotations_name), os.path.join(out_annotations_test, annotations_name))
def split_dataset(img_path, target_folder_path, output_path):
filename = os.listdir(img_path)
np.random.shuffle(filename)
train = filename[:int(len(filename) * 0.9)]
test = filename[int(len(filename) * 0.9):]
out_images = os.path.join(output_path, 'imgs')
os.makedirs(out_images, exist_ok=True)
out_images_train = os.path.join(out_images, 'training')
os.makedirs(out_images_train, exist_ok=True)
out_images_test = os.path.join(out_images, 'test')
os.makedirs(out_images_test, exist_ok=True)
out_annotations = os.path.join(output_path, 'annotations')
os.makedirs(out_annotations, exist_ok=True)
out_annotations_train = os.path.join(out_annotations, 'training')
os.makedirs(out_annotations_train, exist_ok=True)
out_annotations_test = os.path.join(out_annotations, 'test')
os.makedirs(out_annotations_test, exist_ok=True)
for i in train:
img_src = os.path.join(img_path, i)
img_dst = os.path.join(out_images_train, i)
shutil.copyfile(img_src, img_dst)
annotations_name = "gt_" + i[:-3] + 'txt'
annotations_src = os.path.join(target_folder_path, annotations_name)
annotations_dst = os.path.join(out_annotations_train, annotations_name)
shutil.copyfile(annotations_src, annotations_dst)
for i in test:
img_src = os.path.join(img_path, i)
img_dst = os.path.join(out_images_test, i)
shutil.copyfile(img_src, img_dst)
annotations_name = "gt_" + i[:-3] + 'txt'
annotations_src = os.path.join(target_folder_path, annotations_name)
annotations_dst = os.path.join(out_annotations_test, annotations_name)
shutil.copyfile(annotations_src, annotations_dst)