当前位置:网站首页>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()
边栏推荐
- [Tang Laoshi] C -- encapsulation: member variables and access modifiers
- What is the maximum number of concurrent TCP connections for a server? 65535?
- Etcd raft Based Consistency assurance
- Producer consumer mode (multithreading, use of shared resources)
- Transformation between yaml, Jason and Dict
- The 12th Blue Bridge Cup
- Qtablewidget control of QT
- [postgresql]postgresql custom function returns an instance of table type
- Etcd 基于Raft的一致性保证
- Kubernetes abnormal communication network fault solution ideas
猜你喜欢
Qtablewidget control of QT
Hcie security Day12: supplement the concept of packet filtering and security policy
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
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
Q&A:Transformer, Bert, ELMO, GPT, VIT
2.2 integer
如临现场的视觉感染力,NBA决赛直播还能这样看?
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
Introduction to golang garbage collection
随机推荐
Test changes in Devops mode -- learning and thinking
Global and Chinese market of full authority digital engine control (FADEC) 2022-2028: Research Report on technology, participants, trends, market size and share
9 pyqt5 qscrollarea scroll area and qscrollbar scroll bar
Fingerprint password lock based on Hal Library
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
LabVIEW training
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
Global and Chinese market of high purity copper foil 2022-2028: Research Report on technology, participants, trends, market size and share
Rhcsa third day notes
18、 MySQL -- index
Test panghu was teaching you how to use the technical code to flirt with girls online on Valentine's Day 520
Apprentissage intensif - notes d'apprentissage 1 | concepts de base
Camera calibration (I): robot hand eye calibration
Wargames study notes -- Leviathan
Refer to some books for the distinction between blocking, non blocking and synchronous asynchronous
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
Global and Chinese markets of lithium chloride 2022-2028: Research Report on technology, participants, trends, market size and share
How can the outside world get values when using nodejs to link MySQL
Global and Chinese markets of active matrix LCD 2022-2028: Research Report on technology, participants, trends, market size and share
For in, foreach, for of