惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
罗磊的独立博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
A
About on SuperTechFans
WordPress大学
WordPress大学
B
Blog
Martin Fowler
Martin Fowler
Jina AI
Jina AI
I
InfoQ
P
Proofpoint News Feed
小众软件
小众软件
S
SegmentFault 最新的问题
V
V2EX
B
Blog RSS Feed
量子位
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
博客园 - 三生石上(FineUI控件)
MongoDB | Blog
MongoDB | Blog
美团技术团队

博客园 - Lunais

恢复 git stash drop 丢失的内容 Git技巧:彻底重置本地仓库与远程同步,同时保留Stash内容 信源编码和信道编码区别 Leetcode scanf 一组数据差值之和最大 5G 3gpp协议 eclipse调试配置 find查找 浮点类型(float、double)在内存中的存储 yield [基础函数]memset用法 python2处理表格文件按照规则多行输出(中文路径,print) linux系统objdump输出动态库.so文件和静态库.a中符号表 Linux C下的正则表达式 kill eclipse C语言之表达式运算整体提升 查找函数对比:findall,search,match Linux backtrace() git本地协同
python复制删除文件&修改目录&执行bat(wins)
Lunais · 2021-04-07 · via 博客园 - Lunais
 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'))