Python多程序Copy檔案

2021-10-01 01:45:11 字數 2464 閱讀 5813

import os

import time

def readallfiles(dirname):

"""獲取指定目錄的所有內容;"""

all_files =

# 1. 盤頓目錄是否存在?

if os.path.exists(dirname):

# 依次遍歷目錄裡面的所有內容;

for root, dirs, files in os.walk(dirname):

for file in files:

# 將檔案的路徑加入到列表中;

return all_files

else:

print("%s 不存在" % (dirname))

print(readallfiles('/home/kiosk/flask_movie_site-master'))

def copyfiletask(olddir, newdir, file):

"""備份檔案和目錄"""

#把字串中的 old(舊字串) 替換成 new(新字串)

newfile = file.replace(olddir, newdir)

#print(newfile)

# 假設檔案所在的目錄不存在, 先建立;

newfiledirname = os.path.dirname(newfile)

try:

if not os.path.exists(newfiledirname):

os.makedirs(newfiledirname)

fr = open(file, 'rb')

fw = open(file.replace(olddir, newdir), 'wb')

content = fr.read()

fw.write(content)

fr.close()

fw.close()

except exception as e:

print(e)

else:

# print("backup %s success" % (file))

pass##

def main():

# olddir = input("請輸入備份的目錄:")

olddir = '/home/kiosk/flask_movie_site-master'

if os.path.exists(olddir):

try:

date = os.popen('date +%f').read().strip()

newdir = olddir + '-backup-' + date

os.makedirs(newdir)

except fileexistserror as e:

print(e)

else:

print("備份目錄%s已經建立" % (newdir))

all_back_files = readallfiles(olddir)

for file in all_back_files:

copyfiletask(olddir, newdir, file)

##多程序copy

# from concurrent.futures import threadpoolexecutor

# pool = threadpoolexecutor(max_workers=4)

# all_back_files = readallfiles(olddir)

## for file in all_back_files:

# pool.submit(copyfiletask, olddir, newdir, file)

else:

print("error!")

if __name__ == '__main__':

start = time.time()

main()

end = time.time()

print("run %s" % (end - start))

1.非多程序:

2.多程序:

##多程序copy

3.copy好的檔案:

python核心 多程序copy檔案

1.獲取要copy的資料夾的姓名 2.建立乙個資料夾 3.獲取old資料夾中的所有的檔案名字 4.使用多程序的方式copy原資料夾中的所有檔案到新資料夾中 匯入需要的模組 from multiprocessing import pool,manager import os def copyfilet...

多程序目錄檔案copy器python

乙個多程序拷貝某個目錄下的檔案的程式 有進度顯示 import os import multiprocessing defcopy file q,file name,old folder name,new folder name 完成檔案的複製的任務 print 模擬拷貝檔案 s s 檔名 s ol...

Python資料夾copy器(多程序版)

import multiprocessing import os import time import random defcopy file queue,file name,source folder name,dest folder name copy檔案到指定的路徑 f read open s...