定時複製遠端資料夾中所有檔案(Python)

2021-08-20 05:30:31 字數 2486 閱讀 7237

import os, shutil, sys

import threading

import configparser

import datetime

#複製檔案

def remote_copy(src_path, dst_path):

start_time = datetime.datetime.now()

print(start_time, " 開始複製……")

bcopy = false;

try:

# 獲取源資料夾中的所有檔案及資料夾

files = os.listdir(src_path)

for file in files:

#生成絕對路徑

src_file = os.path.join(src_path,file)

# 判斷是否為檔案

if os.path.isfile(src_file) and (os.path.getsize(src_file) < file_size) :

dst_file = os.path.join(dst_path,file)

if not os.path.exists(dst_file):

bcopy = true

shutil.copyfile(src_file, dst_file)

'''copy_cmd = 'xcopy /d %s %s'%(src_file,dst_file)

os.popen(copy_cmd)

'''print(src_file, ' => ', dst_file, 'copy done!')

#else:

# print(dst_file, "已存在!")

except exception as e:

print("無法發現檔案,請檢查網路連線!")

os.system('pause')

sys.exit()

else:

if not bcopy:

print("未發現新檔案……")

end_time = datetime.datetime.now()

'''time = span - (end_time - start_time).seconds

print(end_time, " 本次執行完畢,等待", time, "秒……")'''

print(end_time, " 本次執行完畢,等待", span, "秒……")

#定時複製

def timer_copy(src_path, dst_path):

remote_copy(src_path, dst_path)

global timer

timer = threading.timer(span, timer_copy, [src_path, dst_path])

timer.start()

# 程式入口

if __name__ == "__main__":

#讀取配置檔案

config = configparser.configparser()

config.read("config.ini") 

src_path = config.get('path', 'srcpath')

dst_path = config.get('path', 'dstpath')

global span

span = config.getint('run', 'timespan')

global file_size

file_size = config.getint('run', 'filesize')

# 目的路徑不存在則建立路徑

if not os.path.exists(dst_path):

os.makedirs(dst_path)

print("配置檔案為  :config.ini")

print("執行間隔為  :", span)

print("檔案限制為  :", file_size)

print("輸入資料夾為:", src_path)

print("輸出資料夾為:", dst_path)

inp = input("是否繼續(y/n):")

if inp == 'y' or inp == 'y':

timer = threading.timer(1, timer_copy, [src_path, dst_path])

timer.start()

#測試#remote_copy(src_path, dst_path)

else:

sys.exit()

配置檔案config.ini

[run]

timespan=20000

filesize=5000

[path]

srcpath=\\192.168.0.108\***x\

dstpath=f:\downloads\

C 獲取資料夾中所有檔案

獲取資料夾中的檔案,用到過很多次,每次用的時候都要去查下,很煩,所以想自己寫下,當然,借鑑了很多其他大佬的部落格 主要實現的函式,如下 1 void getfiles string path,vector files 2 19else 20 23 while findnext hfile,filei...

python下遍歷資料夾中所有檔案

python下遍歷某個路徑所有檔案是很常用的事,一直對其有所困擾,今天想明白了之後記錄下來,供以後查閱。首先,資料夾是這樣的 a,b,c是各包含一張jpg的資料夾,其餘是4張jpg。遍歷資料夾的方法首先需要呼叫os庫,即 import os 然後使用 os.walk path path是指想遍歷資料...

檔案 資料夾複製

通道對通道複製效率高 param f1 原始檔 param f2 目的檔案 return throws exception public static long forchannel file f1,file f2 throws exception if inc.size inc.position ...