python復制文件夾到另一個文件夾,python批量復制文件夾下所有文件_python定時復制遠程文件夾中所有文件

 2023-12-25 阅读 28 评论 0

摘要:本文實例為大家分享了python定時復制遠程文件夾中文件的具體代碼,供大家參考,具體內容如下import os, shutil, sysimport threadingimport configparserpython復制文件夾到另一個文件夾、import datetime#復制文件def remote_copy(src_path, dst_path):start_ti

本文實例為大家分享了python定時復制遠程文件夾中文件的具體代碼,供大家參考,具體內容如下

import os, shutil, sys

import threading

import configparser

python復制文件夾到另一個文件夾、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:

ubuntu復制文件夾下所有文件、#生成絕對路徑

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)

python復制文件。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, "已存在!")

python搜索文件夾下的文件、except Exception as e:

print("無法發現文件,請檢查網絡連接!")

os.system('pause')

sys.exit()

else:

python 復制文件到指定文件夾?if not bCopy:

print("未發現新文件……")

end_time = datetime.datetime.now()

'''

time = span - (end_time - start_time).seconds

python 文件夾?print(end_time, " 本次執行完畢,等待", time, "秒……")'''

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

#定時復制

def timer_copy(src_path, dst_path):

remote_copy(src_path, dst_path)

python 文件夾下所有文件?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\xxxx\

dstPath=f:\downloads\

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/194624.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息