一.创建目录,文档等
1.查看当前路径
import os #导入os模块
cur_loc= os.getcwd() #调用os模块的 getcwd()方法。返回值为程序工作的当前目录
print(‘当前目录为:’, cur_loc) #打印目录
2.创建目录
path=os.getcwd()+'/'+'hello'
if not os.path.exists(path) : #没有这个文件目录则新建一个
os.mkdir(path ) #创建G盘文件名为 hello 的文件夹
3.改变工作目录
os.chdir(path)
os.getcwd()
4.在改变后的目录下创建一个txt的文件
f = open('a.txt', 'w')
f.write('hello,')
f.write('iplaypython')
f.close()
在改变后的目录下创建k个txt文件,并写入
for j in range(k):
with open('diff_match%d.txt'% j,'w+') as temp:
temp.write('hello,')
temp.close()
如果只想创建备用,不写入,只是open即可。重新open的时候,旧的写入结果会被覆盖
参考链接
二 . 读写模块
# 读取总txt文件
# 按行读取
open_diff = open('diff_match_image.txt', 'r')
diff_line = open_diff.readlines()
line_list = []
for line in diff_line:
line_list.append(line)
# print(line_list)
#切分diff
diff_match_split = [line_list[i:i+100] for i in range(0,len(line_list),100)]
#将切分的写入多个txt中
for i,j in zip(range(0,13),range(0,13)):
with open('diff_match%d.txt'% j,'w+') as temp:
for line in diff_match_split [i]:
temp.write(line)