python复制删除文件&修改目录&执行bat(wins)

 1 # -*- coding: utf-8 -*-
 2 import os,shutil
 3 #设定文件路径
 4 path = 'D:\lunais'
 5 
 6 #对目录下的文件进行遍历
 7 for file in os.listdir(path):
 8     if os.path.isfile(os.path.join(path,file))==True:
 9         if file.endswith('.axf'):
10             new_name1 = 'name1.axf'
11             new_name2 = 'name1.axf'
12             print 'change name ', new_name1, new_name2, 'success'
13             shutil.copy(os.path.join(path,file),os.path.join(path,new_name1))
14             shutil.copy(os.path.join(path,file),os.path.join(path,new_name2))
15         if file == 'name.elf':
16             new_name1 = 'name1.elf'
17             new_name2 = 'name2.elf'
18             print 'change name ', new_name1, new_name2, 'success'
19             shutil.copy(os.path.join(path,file),os.path.join(path,new_name1))
20             shutil.copy(os.path.join(path,file),os.path.join(path,new_name2))
21 
22 os.remove(path+'\name.axf')
23 os.remove(path+'\name.elf')
os.chdir(path) #修改目录为path
os.chdir('..\') #获取当前目录的上一级目录
print os.getcwd() #获取当前目录
os.system('abc.bat') #执行bat文件

for dirs in os.listdir(path):
    if os.path.isdir(dirs):
        for file in os.listdir(path+'/'+dirs):
            fout = open(path + '/a.txt', 'ab+')
            fin = open(os.path.join(path + '/' + dirs,file), 'rb')
            fout.write(fin.read())
            fin.close()
            fout.flush()
            fout.close()

copy(src, dest)会将目标文件覆盖,write可以使用追加方式写入

for dirs in os.listdir(path):
# print dirs
if os.path.isdir(dirs):
# print dirs
for file in os.listdir(path+'/'+dirs):
fout = open(path + '/uda.xml', 'ab+')
fin = open(os.path.join(path + '/' + dirs,file), 'rb')
fout.write(fin.read())
fin.close()
fout.flush()
fout.close()
# shutil.copy(os.path.join(path + '/' + dirs,file), os.path.join(path, 'uda.xml'))
原文地址:https://www.cnblogs.com/Lunais/p/14628035.html