批量去除首行内容
怎么创建多个文本并写入多行数据呢
上特产:
import os
path = "C:\\Users\\Administrator\\Desktop\\text_1\\"
numb = int(input("需要创建几个文件:"))
a = 1
for i in range(numb):
f = open(path + 'text' + '_' + str(a) + '.txt', 'w')
f.write('这是第' + str(a) + '文件' + '\n')
f.write('这是第' + str(a) + '文件' + '\n')
a += 1
print('创建' + str(numb) + '文件完成!')
创建后的文本效果:
批量去除首行代码:
准备工作
import os
path_1 = "C:\\Users\\Administrator\\Desktop\\text_1\\"
path_2 = "C:\\Users\\Administrator\\Desktop\\text_2\\"
file_names = os.listdir(path_1)
a = 0
for file_name in file_names:
with open(path_1 + file_name, 'r')as f:
with open(path_2 + file_name, 'w')as w:
lines = f.readlines()[1:]
for line in lines:
w.write(line)
a += 1
print('第' + str(a) + '个文件已经完成操作')
看效果:
PS:持续更新