Recursively copy files from the directory directly under the directory using Python

Introduction

--When processing files recursively from the directory directly under the directory, use shell commands because you are basically using the Linux environment. ――However, when recursively copying files in an environment that can only be used in the Windows environment, it was very troublesome to use the command prompt, so I will introduce an example of copying using Python.

Implementation

--Give the copy source path and copy destination path as function arguments --I think you can give the input and output paths as arguments when executing the file. --As a file structure, consider the following example

C:/Users/input/ ├ 01   ├ 01_01.jpg   ├ 01_02.jpg ├ 02   ├ 02_01.jpg   ├ 02_02.jpg

--Copy the above JPG

--Function

import os
import glob
import shutil

def copyfiles(input, output):
    ifiles = os.listdir(input)
    for s in ifiles:
        ifiles_all = input+ "/" + s
        fs = glob.glob(ifiles_all + "/*")
        for f in fs:
            fname = f.split("\\")[-1] #name of file
            ofullname = output + "/" + fname
            shutil.copyfile(f, ofullname)

input = "C:/Users/input"
output = "C:/Users/output"
copyfiles(input, output)

Recommended Posts

Recursively copy files from the directory directly under the directory using Python
Copy S3 files from Python to GCS using GSUtil
Delete all pyc files under the specified directory
Operate the schedule app using python from iphone
Try accessing the YQL API directly from Python 3
Unzip all zip files under the current directory
Copy files directly from Amazon EC2 (Amazon linux) to S3
Import classes in jar files directly from Python scripts
Study from the beginning of Python Hour8: Using packages
Get files from Linux using paramiko and scp [Python]
A little bit from Python using the Jenkins API
Flatten using Python yield from
I tried using the Python library from Ruby with PyCall
File access under the directory
Check what the character code is for all files under the directory that is Python and output
[Linux] Directory under the root
Mac Linux Check the capacity directly under a specific directory
How to get followers and followers from python using the Mastodon API
Operate Maya from an external Python interpreter using the rpyc module
Regularly upload files to Google Drive using the Google Drive API in Python
[Python] Loading csv files using pandas
Recursively unzip zip files with python
Using Rstan from Python with PypeR
Notes on using MeCab from Python
Existence from the viewpoint of Python
Read and use Python files from Python
Using Cloud Storage from Python3 (Introduction)
Use the Flickr API from Python
Extract the targz file using python
Try using the Python Cmd module
Run Ansible from Python using API
Precautions when using phantomjs from python
Access spreadsheets using OAuth 2.0 from Python
Command for the current directory Python
Try using Amazon DynamoDB from Python
Extract strings from files in Python
List all files under the current directory line by line with full path
Try using the Python web framework Django (1)-From installation to server startup
Search for synonyms from the word list (csv) using Python Japanese WordNet
[python] Move files that meet the conditions
[AWS] Using ini files with Lambda [Python]
Try using the Wunderlist API in Python
From Python to using MeCab (and CaboCha)
Play audio files from Python with interrupts
Try using the Kraken API in Python
Behind the flyer: Using Docker with Python
Download the file from S3 using boto.
Learning notes from the beginning of Python 1
Tweet using the Twitter API in Python
Decrypt files encrypted with openssl from python with openssl
Working with OpenStack using the Python SDK
I tried using UnityCloudBuild API from Python
Download files on the web with Python
Reboot the router using Python, Selenium, PhantomJS
Launch the Python interpreter from Git bash
Install Python3 using pythonz under macports environment
From Python 3.4, pip becomes the standard installer! ??
Execute the COPY command using python's Psycopg
Download files in any format using Python
Import by directly specifying the directory path
Learning notes from the beginning of Python 2