Python 基础题

1. 将当前目录文件全部拷贝

# -*- coding:utf-8 -*-

from time import sleep
from multiprocessing import Process
from multiprocessing import Pool 
import os
import time
import random

def copy(rPath,wPath):  # 复制单个文件,从rPath 到 wPath 
    with open(rPath,'rb') as fr:
        print( type(fr))
        with open(wPath,'wb') as fw:
            print( type(fw))
            fw.write(fr.read())     # 将rPath 所有数据拷贝过去

def copy_from_to(listdir): # 将当目录所有的文件复制到 copy 文件下
    curpath = os.getcwd()

    for file in listdir :
        if( os.path.isfile(file) ): # 如果当前目录时文件
            filepath = os.path.join(curpath,file)
            with open(filepath,'rb') as fr:
                topathDir = os.path.join(curpath,'copy')
                topath = os.path.join(topathDir,file)
                with open(topath,'wb') as fw:
                    fw.write(fr.read())

def main():
    copy('data.txt','E:\pyProject\copy\data.txt')


if __name__=='__main__':
    # main()

    copy_from_to(os.listdir(os.getcwd()))       # 将当前目录下所有的文件复制到当前路径下的 "copy" 下 

猜你喜欢

转载自blog.csdn.net/hi_bigguy/article/details/80627468