Python File manipulation functions

Source: Internet
Author: User

This blog is Building powerful image classification models using very little data, which is used to place the picture data in the specified folder as instructed in the tutorial.

Python file processing mainly uses the OS module and the Shutil module, ' sh ' is probably the meaning of bash

Os.chdir (' path ') changes the current path into path

Os.listdir (' path ') outputs all file names under the path path

Os.makedirs (' path/dirname ') create DirName folder under Path path

Shutil.copy2 (' Path/filename ', ' NewPath ') copies the filename file under path to the NewPath path

Scene: According to the above tutorial, we need to download the Kaggle Cat and Dog recognition contest source files, divided into train and test1, unzip and put into the Cat_dog folder, and then we will build a directory that conforms to the requirements of the tutorial, its structure as shown (note the change in file name):

data/    train/        dogs/            dog001.jpg            dog002.jpg            ...        cats/            cat001.jpg            cat002.jpg ...    validation/        dogs/            dog001.jpg            dog002.jpg            ...        cats/            cat001.jpg            cat002.jpg            ...

Depending on the tutorial requirements, you need to copy cat.000.jpg to cat.999.jpg from the train folder to Data/cats/cat000.jpg to Data/cats/cat999.jpg, cat.1000.jpg to cat.1399 . jpg copies to Validation/cats/cat000.jpg to Validation/cats/cat399.jpg. The same structure is used for the construction of the dog data set.

Code:

ImportShutilImportOsos.chdir ('Cat_dog') Os.makedirs ('Train') Os.makedirs ('Train/dogs') Os.makedirs ('Train/cats') Os.makedirs ('Validation') Os.makedirs ('Validation/dogs') Os.makedirs ('Validation/cats')defgetfilename (num, category):returncategory+'.'+STR (num) +'. jpg'defsetfilename (num, category): Num=str (num) num= (3-len (num)) *'0'+Numreturncategory+num+'. jpg' forNuminchRange (1000): Cat= GetFileName (num,'Cat') Dog= GetFileName (num,'Dog') New_cat= Setfilename (num,'Cat') New_dog= Setfilename (num,'Dog') Shutil.copy2 ('train1/'+cat,'train/cats/'+new_cat) Shutil.copy2 ('train1/'+dog,'train/dogs/'+New_dog) forNuminchRange (400): Cat= GetFileName (1000+num,'Cat') Dog= GetFileName (1000+num,'Dog') New_cat= Setfilename (num,'Cat') New_dog= Setfilename (num,'Dog') Shutil.copy2 ('train1/'+cat,'validation/cats/'+new_cat) Shutil.copy2 ('train1/'+dog,'validation/dogs/'+new_dog)

PS: In order to make the build train with the previously downloaded Train folder, the previous train folder is changed to Train1.

PPS: did not pay attention to the original tutorial requirements, the construction of the train folder and validation folder should be placed in the data directory, direct manual operation is good.

Python File manipulation functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.