#!/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)
根据txt指定内容的文件复制到新文件中
最新推荐文章于 2022-01-05 15:34:22 发布