Python example of getting the names of all files in a specified folder and writing them into a list

不言
Release: 2018-04-23 15:11:13
Original
5049 people have browsed it

Below I will share with you an example of using python to obtain the names of all files in a specified folder and write them into a list. It has a good reference value and I hope it will be helpful to everyone. Come and take a look together

As shown below:

import os
import os.path
rootdir = "./pic_data"
file_object = open('train_list.txt','w')
for parent,dirnames,filenames in os.walk(rootdir):
 for filename in filenames:
 print filename
  file_object.write(filename+ '\n')
file_object.close()
Copy after login

at When doing deep learning, there will be a large amount of data. For convenience, you can directly copy it to a folder with the command. The code is as follows:

import shutil 
import os
import os.path
rootdir = "./mjsynth/mnt/ramdisk/max/90kDICT32px"
#rootdir = "./123"
def Test2(rootDir): 
 for lists in os.listdir(rootDir): 
 #如果找到的是图片,则打印出来
 if lists[-3:]=='jpg':
  print lists
  path = os.path.join(rootDir, lists) 
  shutil.copy(path,"./500")
  continue
 #如果找到的是文件夹,则判断,如果名称小于2则递归
 if int(lists)<501:
  path = os.path.join(rootDir, lists) 
  if os.path.isdir(path): 
  Test2(path) 
Test2(rootdir)
Copy after login

Related recommendations:

Instances of python getting file path, file name, suffix name

##

The above is the detailed content of Python example of getting the names of all files in a specified folder and writing them into a list. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!