#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import shutil

'''
    判断文件是否存在,存在无效果,不存在则生成文件夹
    path:路径的绝对路径,通过获取当前文件的绝对路径,再将文件夹文件拼接起来
'''
def isexists_dir_Create(path):
     if not os.path.exists(path):
        os.makedirs(path)

def txt_copy_file(file,target_file):
    file_object = open(file)
    lines = file_object.readlines()
    for line in lines:
       line = line.strip()  #要去除字符格式
       print(line)
       if not os.path.exists(line):  # True/False
           pass # 做判断条件 文件不存在
           # print('0')
       else:
           shutil.copy(line,target_file)  #文件存在就copy
           # print('1')

file = './train1.txt'
target_file = './test3/'
isexists_dir_Create(target_file)
txt_copy_file(file,target_file)
Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐