简体   繁体   中英

Python script to copy n files to another folder

I'm looking for a python script that would took for example first 5000 files and copy them to another folder. Then after another run, it would took next 5000 files and copy them.

I tried using shutil but couldn't make it work, no matter what I tried.

Can you please help, or guide me on the right direction?

What you may be looking for is a combination of os.walk() HERE and shutil.copy() HERE

Building a script based on sample provided on those two links looks pretty straight forward.

Good luck.

This should do it for you read the code for help to make it work Change the Mkv format to whatever you want

it's a python script you can use crontab for Linux or Microsoft windows schedule tool if you need any help just reply

import os
import json

SRC_FOLDER = '/home/SOURCE /'
DEST_FOLDER = '/home/Destination folder /'

def read_data():
    with open('/home/PATH TO the json file.json') as f:
        data = json.load(f)
        return data

def write_data(added_files, uploaded_files):
    with open('/home/PATH TO the json file.json', 'w') as f:
        json.dump(added_files+uploaded_files, f)

def main():
    all_downloads = os.listdir(SRC_FOLDER)
    all_uploads = read_data()
    added_files = []
    for file_name in all_downloads:
        if file_name not in all_uploads:
            if "mkv" == file_name.split(".")[-1]:
                print file_name.split('.')[-1]
                added_files.append(file_name)
                file = open(DEST_FOLDER+file_name, 'wb')
                with open(SRC_FOLDER+file_name, 'rb') as f:
                    while True:
                        byte = f.read(20480)
                        if not byte:
                            break
                        file.write(byte)
    write_data(added_files, all_uploads)

if __name__ == '__main__':
    main()

iCODEiT 0UT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM