import os
def add_text(path,filepath:list):
path_list = os.listdir(path)
print(path_list)
for i in path_list:
for p in filepath:
if p == i:
add_path = os.path.join(path,i)
file = os.listdir(add_path)
for i in file:
absolute_path = os.path.join(add_path,i)#绝对路径
if os.path.isfile(absolute_path) :
print(absolute_path)
# if os.path.isfile(i):
# filepath, tempfilename = os.path.split(i) # 拆开
# filename, extension = os.path.splitext(tempfilename)# 获取扩展名
f = open(absolute_path, "a+", encoding="utf8")
f.write("test\n")
f.close()
if __name__ == '__main__':
add_text(r"F:\test",['a','b'])
# filepath, tempfilename = os.path.split(i) # 拆开
# #filename, extension = os.path.splitext(tempfilename)# 获取扩展名
# #if filename[]
如何才能批量执行同一个目录下的python文件下面以批量把tmp文件更名为jpg文件为例,代码如下:
import os
for file in os.listdir("."):
if os.path.splitext(file)[1] == ".tmp":
os.rename(file, os.path.splitext(file)[0]+".jpg"
将代码保存至文件,放至需要对文件进行重命名的文件夹下,运行即可。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)