当前位置:网站首页>Read the root directory of the folder, write txt and generate random samples
Read the root directory of the folder, write txt and generate random samples
2022-07-03 20:48:00 【Bright moon drunk windowsill】
adopt python It is really convenient to write some scripts to realize the data processing function , Record the present first , Follow up supplement and improvement
1. Read all data in the folder and take random samples
notes : Main call os The file operation function of the Library , If you want to master the degree, you can realize a small function by yourself, and you will soon understand its basic principle , When writing code, you need to pay attention to the up and down alignment and spaces , Otherwise, it will report a mistake , Then, be careful about the coding when reading and writing Chinese paths , The other is string operation , The specific implementation is as follows :
# -*- coding:UTF-8 -*-
import os
import random
dirname = "...folder"
txt_path='F:/code/Pycham/list.txt'
txt_file = open(txt_path,'w')
imglist=[]
def traverse_path(file_path):
files = os.listdir(file_path)
for fi in files:
fi_d = os.path.join(file_path, fi)
if os.path.isdir(fi_d):
traverse_path(fi_d)
else:
img_name=os.path.join(file_path, fi_d)
#print(img_name)
if img_name[-4:]=='tiff':
#print(img_name)
txt_file.write(img_name)
txt_file.write('\n')
# for i in len(lists):
# print(lists(i))
#print(" Number of read images :",len(imglist))
traverse_path(dirname)
# Number of random samples
sample_file='F:\code\Pycham\sample.txt'
n=1000
f=open(txt_path,encoding='gbk')
lines=f.readlines()
list=[]
for line in lines:
print(line)
list.append(line)
print(" The total number of samples :",len(list)," Number of samples :",n)
g=open(sample_file,'w')
a=random.sample(list,n)# Random sampling n That's ok
for i in a:
g.write(i)
f.close()
g.close()
边栏推荐
- For in, foreach, for of
- 浅析 Ref-NeRF
- 11-grom-v2-05-initialization
- Offset related concepts + drag modal box case
- 2.6 formula calculation
- "Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks
- String and+
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- 强化学习-学习笔记1 | 基础概念
- 1.4 learn more about functions
猜你喜欢

一台服务器最大并发 tcp 连接数多少?65535?

Task of gradle learning

An old programmer gave it to college students

In 2021, the global foam protection packaging revenue was about $5286.7 million, and it is expected to reach $6615 million in 2028

In 2021, the global revenue of thick film resistors was about $1537.3 million, and it is expected to reach $2118.7 million in 2028

Machine learning support vector machine SVM

Hcie security Day12: supplement the concept of packet filtering and security policy

In 2021, the global revenue of syphilis rapid detection kits was about US $608.1 million, and it is expected to reach US $712.9 million in 2028

Rhcsa third day notes

Qt6 QML Book/Qt Quick 3D/基础知识
随机推荐
Set, weakset, map, weakmap in ES6
Get log4net log file in C - get log4net log file in C
Global and Chinese market of electrolyte analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Use nodejs+express+mongodb to complete the data persistence project (with modified source code)
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
Q&A:Transformer, Bert, ELMO, GPT, VIT
如临现场的视觉感染力,NBA决赛直播还能这样看?
JMeter plug-in installation
Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
AST (Abstract Syntax Tree)
JVM JNI and PVM pybind11 mass data transmission and optimization
Deep search DFS + wide search BFS + traversal of trees and graphs + topological sequence (template article acwing)
Global and Chinese market of speed limiter 2022-2028: Research Report on technology, participants, trends, market size and share
Discussion Net legacy application transformation
Golang type assertion and conversion (and strconv package)
SQL injection - Fundamentals of SQL database operation
Global and Chinese markets of polyimide tubes for electronics 2022-2028: Research Report on technology, participants, trends, market size and share
2.4 conversion of different data types
阻塞非阻塞和同步异步的区分 参考一些书籍
Line segment tree blue book explanation + classic example acwing 1275 Maximum number