当前位置:网站首页>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()
边栏推荐
- Research Report on the overall scale, major manufacturers, major regions, products and application segmentation of rotary tablet presses in the global market in 2022
- Brief analysis of ref nerf
- Rhcsa third day notes
- Global and Chinese market of liquid antifreeze 2022-2028: Research Report on technology, participants, trends, market size and share
- Set, weakset, map, weakmap in ES6
- Basic knowledge of dictionaries and collections
- Global and Chinese markets of cast iron diaphragm valves 2022-2028: Research Report on technology, participants, trends, market size and share
- 你真的知道自己多大了吗?
- 同花顺开户注册安全靠谱吗?有没有风险的?
- Refer to some books for the distinction between blocking, non blocking and synchronous asynchronous
猜你喜欢
2.3 other data types
How to handle wechat circle of friends marketing activities and share production and release skills
How to choose cache read / write strategies in different business scenarios?
The "boss management manual" that is wildly spread all over the network (turn)
如临现场的视觉感染力,NBA决赛直播还能这样看?
jvm jni 及 pvm pybind11 大批量数据传输及优化
1.5 learn to find mistakes first
2.6 formula calculation
2022 high voltage electrician examination and high voltage electrician reexamination examination
Test changes in Devops mode -- learning and thinking
随机推荐
App compliance
Introduction to golang garbage collection
JVM JNI and PVM pybind11 mass data transmission and optimization
Etcd 基于Raft的一致性保证
Pytorch sets the weight and bias of the model to zero
AI enhanced safety monitoring project [with detailed code]
Brief analysis of ref nerf
浅析 Ref-NeRF
Global and Chinese market of high temperature Silver sintering paste 2022-2028: Research Report on technology, participants, trends, market size and share
浅议.NET遗留应用改造
@Transactional注解失效的场景
For in, foreach, for of
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
Cannot load driver class: com. mysql. cj. jdbc. Driver
Qtablewidget control of QT
Rhcsa third day notes
Go learning notes (4) basic types and statements (3)
JS three families
2.6 formula calculation
jvm jni 及 pvm pybind11 大批量数据传输及优化