Write a Python script to implement recursive copy of Files

Source: Internet
Author: User
Write a Python script to recursively copy and reduce the font of a file.
Author of the enlarged Font: Anonymous Source: This site is organized. Release Date: 16:39:34

Today, I suddenly found a folder with lots of photos and videos stored in my computer. I think it was copied last year when I changed my cell phone (popular little 5. Read several images

Pictures and past scenes come to my mind. Well, I am a sensible person. So I want to flip these photos, but copy them to my cell phone.

The folder is automatically divided by time, and it is very troublesome to copy one folder, so I plan to write a Python script to complete this job (pull so much, finally to the topic, continue)

This is the root directory of the folder to be copied. Each subdirectory contains several photos.

Python 2.7.3 official edition: http://www.cr173.com/soft/16395.html

To put it bluntly, go to the Code:

#-*-Coding: UTF-8 -*-
#! /Usr/bin/Python
# Filename: copyfile. py
Import OS, shutil
Def mycopy (srcpath, dstpath ):
If not OS. Path. exists (srcpath ):
Print "srcpath not exist! "
If not OS. Path. exists (dstpath ):
Print "dstpath not exist! "
For root, dirs, files in OS. Walk (srcpath, true ):
For eachfile in files:
Shutil. Copy (OS. Path. Join (root, eachfile), dstpath)
Srcpath = 'e: \ PIC'
Dstpath = 'f: \ pictotal'
Mycopy (srcpath, dstpath)

Run this script and go to disk F to see it:

The photos have been copied, and there are indeed a lot of photos (there are still a lot below, not finished)

The code is easy to understand, mainly OS. walk () function. This function returns the three tuples in the specified path (Starting path, directory in the starting path, and file name list without path name in the starting path)

It can recursively traverse all directories and file names under a specified directory, which is easy to use.

You can also useOS. listdir (dirname): Function. The listdir function lists the directories and files under dirname, and then uses a judgment: if it is a file, copy it; if it is a directory, continue recursion.

It is obviously not convenient to use the walk () function. However, I don't know how the walk () function is implemented internally. It may be difficult to directly store all the files in the root directory in the list neutral performance,

You can use listdir () to compare and test it later.

It can be seen that python only needs a few lines of code to complete this work, which is very convenient. If you use C ++ to implement code, it will be longer than this.

Visible,It doesn't matter whether the language is high or low. It is good to achieve the goal efficiently and conveniently, isn't it?

Related Article

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.